简体   繁体   English

在Python的子流程模块中运行程序,并从Terminal中获取所需文件

[英]Run a program in Python's subprocess module, with required files from Terminal

Is it feasible to run a program in Python's subprocess module, but with files from Terminal? 在Python的子进程模块中运行程序,但使用Terminal中的文件是否可行?

So I want to run the following program from within Python: 因此,我想在Python中运行以下程序:

myProgram -a myArg

However, suppose that the above program requires the file myFile in the current directory, and it doesn't take the required file as an argument. 但是,假设上述程序在当前目录中需要文件myFile ,并且不将所需文件作为参数。 So, if you run the above program in the directory where there is myFile , the program succeeds in processing. 因此,如果在存在myFile的目录中运行上述程序,则该程序将成功处理。 However, if you run it in the directory where there is NOT myFile , the execution fails. 但是,如果在没有myFile的目录中运行它,则执行将失败。

And when I tried to execute the program from within Python's subprocess.Popen() , with shell=True , the program doesn't work and it looks like the reason it failed is the program wasn't able to read myFile when executed from within Python. 当我尝试从Python的subprocess.Popen()中使用shell=True执行该程序时,该程序无法正常工作,并且看起来失败的原因是该程序从内部执行时无法读取myFile 。蟒蛇。

So, is there any way to run it successfully from within Python? 那么,有什么方法可以在Python内部成功运行它吗?

subprocess.Popen('myProgram -a myArg', cwd='/folder/with_myFile/')

类似的问题: Python通过参数指定popen工作目录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM