【发布时间】:2011-10-24 20:18:20
【问题描述】:
当我在 Windows 7 下使用 Python 以附加模式 ('a+') 打开文件时,我看到了奇怪的行为。
我想知道该行为是否实际上不正确,或者我误解了如何使用以下代码:
log_file= open(log_file_path, "a+")
return_code = subprocess.call(["make", target], stdout=log_file, stderr=subprocess.STDOUT)
log_file.close()
上面的代码行没有正确地附加到文件中。事实上,在随后的运行中,它甚至不会修改文件。 我也使用 Python Shell 对其进行了测试。 首次打开文件后,进行多个子进程调用将正确附加到文件中,但是一旦文件关闭并重新打开,它将永远不会再次附加。
谁有线索?
谢谢
更简单的问题这里是另一组将失败的步骤:
log_file=open("temp.txt", "a+")
log_file.write("THIS IS A TEST")
log_file.close()
log_file=open("temp.txt", "a+")
subprocess.call(["echo", "test"], stdout=log_file, stderr=subprocess.STDOUT, shell=True)
log_file.close()
如果你打开文件 temp.txt,我看到的是:
测试
S A MUTHER F** 测试
【问题讨论】:
-
我在下面更新了我的答案,以反映您发布的新信息,减去脏话:)
标签: windows-7 file-io subprocess python