运行以下脚本时,我收到一个奇怪的“拒绝访问-\”错误\警告:
import os
import subprocess
directory = r'S:\ome\directory'
subprocess.run(['find', '/i', '"error"', '*.txt', '>Errors.log'], shell=True, cwd=directory)
我还检查了:
^{pr2}$它们都是print
True
。在
当subprocess
命令正在运行但进程没有终止时,将打印错误消息;不会引发任何内容。因此,即使不指定异常,将其包装成try-except
也不会捕获任何内容。当该过程结束时,将创建文件(Error.log
),但包含错误的结果。在
从在指定目录中打开的cmd运行完全相同的命令(find /i "fatal" *.txt >Error.log
)会产生正确的结果。在
这两种方法在哪方面不同?
方法1(来自Python):
subprocess.run(['find', '/i', '"error"', '*.txt', '>Errors.log'], shell=True, cwd=r'S:\ome\directory')
方法2(来自cmd):
S:\ome\directory>find /i "error" *.txt >Errors.log
尝试传递
stdout
参数的输出文件处理程序:也许,这是实现任务的唯一方法,因为子流程.运行不运行由其参数描述的重定向(>;或>>;)。在
我仍然不确定到底是什么问题,只是改变了:
收件人:
^{pr2}$就这样。在
如图所示,手动缝合该命令起作用。如果有人对此事有更多的消息,我将不胜感激。在
FromPopen构造函数(由调用子流程.运行)在
问题是CreateProcess不支持重定向。在
请参见:How do I redirect output to a file with CreateProcess?
相关问题 更多 >
编程相关推荐