我正在尝试这样的事情,但我不确定为什么它不起作用。
for files in os.listdir("."):
if files.endswith(".o"):
str = "cmp "+ str1+" "+str2 +" > s.txt "
subprocess.Popen(str, shell=True)
如果我没有 te.txt,我会得到输出,但是当我尝试将输出定向到一个文件时,该文件不会被创建。有人能告诉我这里出了什么问题吗?
请您参考如下方法:
试试这个
import os
for files in os.listdir("."):
if files.endswith(".o"):
str = "cmp "+ str1+" "+str2 +" > s.txt "
os.system(str)
如果你使用 subprocess 你需要把所有的参数作为单词列表 例如:
import subprocess
subprocess.Popen(["ls", "-la"])
最后一点,您不需要从 ...> file.txt 中输出,您可以使用 subprocess 来实现。例如
subprocess.Popen(["ls", "-la"], stdout=anyfile)
