gpt4 book ai didi

python - 在 subprocess.Popen 命令中使用变量

转载 作者:太空宇宙 更新时间:2023-11-03 13:37:52 26 4
gpt4 key购买 nike

<分区>

现在我有一个测试文件 .dat,我在其上运行 hexdump 并将输出放入 hexdump.dat 文件。

subprocess.Popen(['hexdump file.dat > hexdump.dat' ], shell=True)  

作为旁注,我看到了不使用 shell=True 的建议,但我实际上得到了错误 OSError: [Errno 2] No such file or directory

因此,我希望能够传入变量或数组、文件,而不是硬编码的“file.dat”。 "file"可以是用户输入或从先前的子流程部分生成的数组/列表。

我试过一个用户输入案例:

from subprocess import Popen, PIPE, STDOUT 
files = raw_input('File Name: ')
p = subprocess.Popen(['hexdump files > hexdump.dat' ], stdout=PIPE, stderr=STDOUT)
out,err = p.communicate(input=files)

还有:

p = subprocess.Popen(['hexdump',  inputs, ' > hexdump.dat' ], stdout=PIPE, stderr=STDOUT)                                          

感谢您的帮助,我知道我没有正确理解此处所需的结构,因此不胜感激。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com