gpt4 book ai didi

python - 如何避免子进程中的 shell=True

转载 作者:行者123 更新时间:2023-12-02 03:01:34 25 4
gpt4 key购买 nike

我有子进程命令来检查 md5 校验和

subprocess.check_output('md5 Downloads/test.txt', stderr=subprocess.STDOUT, shell=True)

效果很好。但我读到尝试避免 shell=True但是当我运行时

subprocess.check_output('md5 Downloads/test.txt', stderr=subprocess.STDOUT, shell=False)

我收到错误OSError:[Errno 2]没有这样的文件或目录

我可以使用 shell=False 运行上述命令或解决方法吗?或者保留 shell=True 也可以吗?

最佳答案

只需将参数作为列表传递给check_output():

subprocess.check_output(["md5", "Downloads/test.txt"], stderr=subprocess.STDOUT)

来自docs :

args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments.

关于python - 如何避免子进程中的 shell=True,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48100820/

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