Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何让python(2.5版)脚本在文件夹中而不是从命令行运行jar文件?_Python - Fatal编程技术网

如何让python(2.5版)脚本在文件夹中而不是从命令行运行jar文件?

如何让python(2.5版)脚本在文件夹中而不是从命令行运行jar文件?,python,Python,我熟悉使用os.system从命令行运行。但是,我希望能够从特定文件夹(例如我的“test”文件夹)内部运行jar文件。这是因为我的jar(位于我的“test”文件夹中)需要在我的“test”文件夹中有一个文件。那么,如何在脚本中编写一个函数来执行以下操作:c:\test>java-jar run\u this.jar required\u parameter.ext?我是python新手,因此非常感谢您提供详细信息。提前谢谢 一般情况下:使用os.chdir更改父进程的目录,然后使用os.sy

我熟悉使用os.system从命令行运行。但是,我希望能够从特定文件夹(例如我的“test”文件夹)内部运行jar文件。这是因为我的jar(位于我的“test”文件夹中)需要在我的“test”文件夹中有一个文件。那么,如何在脚本中编写一个函数来执行以下操作:
c:\test>java-jar run\u this.jar required\u parameter.ext
?我是python新手,因此非常感谢您提供详细信息。提前谢谢

一般情况下:使用os.chdir更改父进程的目录,然后使用os.system运行jar文件。如果需要保持Python的工作目录稳定,则需要将chdir返回到原始工作目录-需要使用os.getcwd()记录


在Unix上:显式使用os.fork创建子进程。在父级中,使用os.waitpid等待子级。在子系统中,使用os.chdir,然后使用os.exec运行java。

下面是一个小脚本,让您开始使用。有很多方法可以让它“更好”,但是不知道你想要完成的事情的全部范围就足够了

import os

if __name__ == "__main__":
   startingDir = os.getcwd() # save our current directory
   testDir = "\\test" # note that \ is windows specific, and we have to escape it
   os.chdir(testDir) # change to our test directory
   os.system("java -jar run_this.jar required_paramter.ext")
   os.chdir(startingDir) # change back to where we started

尝试提供脚本中不起作用的部分。不是所有的,而是一段说明您的问题的代码。为什么os.system优先于SUBSPROCESS.Popen?我个人认为对于初学者来说,SUBSPROCESS API过载过多(即使它允许指定新进程的cwd,这使得恢复变得不必要)。