Reputation: 110502
I need to cd into a particular directory to run a jar package. This is what I'm currently trying to do, albeit unsucessfully:
subprocess.call(['cd','sales'])
subprocess.call(shlex.split("""java Autoingestion %s %s %s Sales Daily Details %s"""%
(self.username, self.password, self.vendor_number, self.date)))
How would I correctly cd into the sales folder and execute this script?
Upvotes: 0
Views: 247
Reputation: 4260
Use os.chdir() to change the current directory to a specified one.
I would suggest you to use subprocess.Popen() rather than subprocess.call(). If you wish to set the environment yourself before running the java code, like setting JAVA_PATH, etc, it is easy by setting the env argument of Popen.
from subprocess import Popen
from os import chdir
from os.path import exists
sales_dir = "/home/ubuntu/sales"
# Sanity check for the directory
if exists(sales_dir):
chdir(sales_dir)
new_proces = Popen("java Autoingestion %s %s %s Sales Daily Details %s" %
(self.username, self.password, self.vendor_number, self.date))
Upvotes: 0
Reputation: 11235
You should do
subprocess.call(["java","Autoingestion",self.username, self.password, self.vendor_number, "Sales","Daily","Details",self.date, cwd="sales")
Notice that you should not do shlex.split as it is insecure
Upvotes: 2
Reputation: 208665
Use os.chdir()
before your second Popen
call (and get rid of the first Popen
call).
Upvotes: 1