如何使用 python 将文本附加到文件?
How do I append text to a file with python?
我正在尝试创建一个可以连续添加“../”的文件。我的代码如下:
with open("/tmp/newfile.txt", "a+") as myfile:
myfile.write('../')
contents = myfile.read()
print(contents)
然而,当我运行这个代码时returns<empty>
追加文件:
with open("newfile.txt", "a+") as file:
file.write("I am adding in more lines\n")
file.write("And more…")
读取文件:
with open('newfile.txt') as f:
lines = f.readlines()
print(lines)
我正在尝试创建一个可以连续添加“../”的文件。我的代码如下:
with open("/tmp/newfile.txt", "a+") as myfile:
myfile.write('../')
contents = myfile.read()
print(contents)
然而,当我运行这个代码时returns<empty>
追加文件:
with open("newfile.txt", "a+") as file:
file.write("I am adding in more lines\n")
file.write("And more…")
读取文件:
with open('newfile.txt') as f:
lines = f.readlines()
print(lines)