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

Ama*_*ara 3 python

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

gri*_*eve 5

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

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
Run Code Online (Sandbox Code Playgroud)