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

时间:2020-11-30 07:08:40

I am familiar with using the os.system to run from the command line. However, I would like to be able to run a jar file from inside of a specific folder, eg. my 'test' folder. This is because my jar (located in my 'test' folder) requires a file inside of my 'test' folder. So, how would I write a function in my script that does the following: c:\test>java -jar run_this.jar required_parameter.ext ? I'm a python newbie so details are greatly appreciated. Thanks in advance.

我熟悉使用os.system从命令行运行。但是,我希望能够从特定文件夹内部运行jar文件,例如。我的'test'文件夹。这是因为我的jar(位于我的'test'文件夹中)需要我的'test'文件夹中的文件。那么,我如何在我的脚本中编写一个函数来执行以下操作:c:\ test> java -jar run_this.jar required_pa​​rameter.ext?我是一个蟒蛇新手,所以细节非常感谢。提前致谢。

2 个解决方案

#1


5  

Here is a small script to get you started. There are ways to make it "better", but not knowing the full scope of what you are trying to accomplish this should be sufficient.

这是一个小程序,可以帮助您入门。有办法让它“更好”,但不知道你想要实现的全部范围应该是足够的。

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

#2


1  

In general: Use os.chdir to change the directory of the parent process, then os.system to run the jar file. If you need to keep Python's working directory stable, you need to chdir back to original working directory - you need to record that with os.getcwd().

通常:使用os.chdir更改父进程的目录,然后使用os.system运行jar文件。如果你需要保持Python的工作目录稳定,你需要chdir回到原始工作目录 - 你需要用os.getcwd()记录它。

On Unix: Create a child process with os.fork explicitly. In the parent, wait for the child with os.waitpid. In the child, use os.chdir, then os.exec to run java.

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

#1


5  

Here is a small script to get you started. There are ways to make it "better", but not knowing the full scope of what you are trying to accomplish this should be sufficient.

这是一个小程序,可以帮助您入门。有办法让它“更好”,但不知道你想要实现的全部范围应该是足够的。

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

#2


1  

In general: Use os.chdir to change the directory of the parent process, then os.system to run the jar file. If you need to keep Python's working directory stable, you need to chdir back to original working directory - you need to record that with os.getcwd().

通常:使用os.chdir更改父进程的目录,然后使用os.system运行jar文件。如果你需要保持Python的工作目录稳定,你需要chdir回到原始工作目录 - 你需要用os.getcwd()记录它。

On Unix: Create a child process with os.fork explicitly. In the parent, wait for the child with os.waitpid. In the child, use os.chdir, then os.exec to run java.

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