window 7, python 2.7.2
以下运行没有错误:
from subprocess import call
f = open("file1","w")
f.writelines("sigh")
f.flush
f.close
call("copy file1 + file2 file3", shell=True)
但是,file3 只包含 file2 的内容。 file1 和 file2 名称都像 Windows 一样被回显,但是,调用副本时 file1 似乎是空的。好像 file1 还没有完全写入和刷新。如果 file1 是单独创建的,而不是在同一个 python 文件中创建的,则以下命令将按预期运行:
from subprocess import call
call("copy file1 + file2 file3", shell=True)
抱歉,如果这里要怪 python 新手。非常感谢任何帮助。
请您参考如下方法:
您缺少括号:
f.flush()
f.close()
您的代码在语法上是有效的,但没有调用这两个函数。
编写该序列的更 Pythonic 方式是:
with open("file1","w") as f:
f.write("sigh\n") # don't use writelines() for one lonely string
call("copy file1 + file2 file3", shell=True)
这将自动关闭 with block 末尾的 f(并且 flush() 无论如何都是多余的)。
