生成读写文件的python代码、
时间: 2023-03-28 19:04:08 浏览: 130
Python读写文件
可以使用以下代码来生成和读写文件:
# 生成文件
with open('file.txt', 'w') as f:
f.write('Hello, world!')
# 读取文件
with open('file.txt', 'r') as f:
content = f.read()
print(content)
# 追加内容到文件
with open('file.txt', 'a') as f:
f.write('\nThis is a new line.')
# 读取文件
with open('file.txt', 'r') as f:
content = f.read()
print(content)
阅读全文