python 子进程不应 运行 来自文件夹
python subprocess shouldn't run from within the folder
我正在尝试在我的 mac 中使用 C&C NLP 库,它使用终端作为其界面。所以我很自然地尝试 运行 来自 python 的命令,但这是发生的事情:
candc:could not open model configuration file for reading:models/config
原来不应该从同一目录调用 candc,而应该从二进制文件夹外部调用,类似于 "bin/candc"。
我怎样才能完成这项工作?
这是我的代码:
cmd="candc/bin/candc --models models"
subprocess.check_output('{} | tee /dev/stderr'.format( cmd ), shell=True)
使用 cmd
中的完整路径:
cmd = "/home/your-username/python-programs/cnc/candc/bin/canc --models models
无论完整路径是什么。您可以在 candc
目录中使用(如果您在 linux)pwd
来找出它是什么。
将 cwd
参数与您想要的工作目录一起传递。
例如,如果您想要 运行 它作为 candc
目录中的 bin/candc
:
import os
cmd="bin/candc --models models"
subprocess.check_output('{} | tee /dev/stderr'.format( cmd ), shell=True, cwd=os.path.abspath('candc'))
(我不确定你是否真的需要 os.path.abspath。使用和不使用它都要进行测试。)
我正在尝试在我的 mac 中使用 C&C NLP 库,它使用终端作为其界面。所以我很自然地尝试 运行 来自 python 的命令,但这是发生的事情:
candc:could not open model configuration file for reading:models/config
原来不应该从同一目录调用 candc,而应该从二进制文件夹外部调用,类似于 "bin/candc"。 我怎样才能完成这项工作?
这是我的代码:
cmd="candc/bin/candc --models models"
subprocess.check_output('{} | tee /dev/stderr'.format( cmd ), shell=True)
使用 cmd
中的完整路径:
cmd = "/home/your-username/python-programs/cnc/candc/bin/canc --models models
无论完整路径是什么。您可以在 candc
目录中使用(如果您在 linux)pwd
来找出它是什么。
将 cwd
参数与您想要的工作目录一起传递。
例如,如果您想要 运行 它作为 candc
目录中的 bin/candc
:
import os
cmd="bin/candc --models models"
subprocess.check_output('{} | tee /dev/stderr'.format( cmd ), shell=True, cwd=os.path.abspath('candc'))
(我不确定你是否真的需要 os.path.abspath。使用和不使用它都要进行测试。)