我知道对子进程使用 shell=True 是不好的做法。但是对于这行代码,我不确定如何使用 shell=False 执行它
subprocess.Popen('candump -tA can0 can1 >> %s' %(file_name), shell=True)
我要运行的命令是:
candump -tA can0 can1 >> file_name
Where file_name
is /path/to/file.log
您不能像使用命令一样直接在命令中使用管道shell=True
,但很容易适应:
with open(file_name, 'ab') as outf:
proc = subprocess.Popen(['candump', '-tA', 'can0', 'can1'], stdout=outf)
这会在 Python 级别打开文件进行二进制追加,并将其作为stdout
对于子流程。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)