Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a file that requires being in the same directory?

Tags:

python

I have a python script that needs to execute a .jar file that is located in another directory. What would be the best way to do this? So far I was thinking -

subprocess.call(["cd","/path/to/file"])
subprocess.call(["./file.jar"])

How should I do this?

Update:

Using both of the answers below, this is what I ended up doing:

subprocess.call(shlex.split("./file.jar -rest -of -command"), cwd=COMMAND_FOLDER)
like image 405
David542 Avatar asked Dec 07 '25 07:12

David542


1 Answers

To run a process in a different current working directory, use subprocess.Popen's cwd parameter:

import subprocess
proc = subprocess.Popen(['file.jar'], cwd = '/path/to/file')
like image 140
unutbu Avatar answered Dec 08 '25 21:12

unutbu