gpt4 book ai didi

python - 如何从 python 运行命令行

转载 作者:行者123 更新时间:2023-12-01 04:43:40 24 4
gpt4 key购买 nike

我在终端中运行以下命令:

mongoexport --db database_name --collection agents --type=csv --fieldFile agFieldsTest.txt --out file/path/agTestInfo.csv

我尝试使用以下方式运行它:

>>> import subprocess
>>> subprocess.call(["mongoexport --db database_name --collection agents --type=csv --fieldFile agFieldsTest.txt --out file/path/agTestInfo.csv"])

我收到以下错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 524, in call
return Popen(*popenargs, **kwargs).wait()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1308, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

最佳答案

最好的方法是将命令分解为单独的“单词”:

>>> subprocess.call(["mongoexport", "--db", "database_name", "--collection", "agents", "--type=csv", "--fieldFile", "agFieldsTest.txt", "--out", "file/path/agTestInfo.csv"])

或者,您可以使用 shell=True 让 shell 为您执行此操作:

>>> subprocess.call(["mongoexport --db database_name --collection agents --type=csv --fieldFile agFieldsTest.txt --out file/path/agTestInfo.csv"], shell=True)

关于python - 如何从 python 运行命令行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29976350/

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