【发布时间】:2015-06-17 18:48:11
【问题描述】:
我想在 python 中的现有文件中添加行。我写了以下两个文件
print_lines.py
while True:
curr_file = open('myfile',r)
lines = curr_file.readlines()
for line in lines:
print lines
add_lines.py
curr_file = open('myfile',w)
curr_file.write('hello world')
curr_file.close()
但是当我先运行 print_lines.py 然后运行 add_lines.py 时,我没有得到我添加的新行。我该如何解决?
【问题讨论】:
-
代码中存在语法错误。 open() 接受字符参数,例如 open('myfile', 'w')
-
open('myfile', 'w')应该以附加模式打开文件open('myfile','a')
标签: python file python-2.7 io