建议使用 os.system 来执行来自 python 脚本的命令。此外,据称重定向操作员在那里工作。例如这里和这里。我愿意
os.system("ls > out.txt")
这确实适用于我的一台计算机。另一个生产
ls: cannot access >: No such file or directory
ls: cannot access out.txt: No such file or directory
另一方面,我有权调查哪个进程产生此消息。但是os.system("ls")
列出了像魅力一样的文件。两者都是Windows 7机器。
错误没有...正如 Martijn 评论的那样 - 不建议使用 - 使用 subprocess
,例如:
import subprocess
with open('myfile.txt', 'w') as fout:
subprocess.check_call('ls', stdout=fout)