I'm calling a command line program in python using the os.system(command)
call.
How can I call this command passing a different folder for execution? There is a system call for this? Or I should save the current folder, and, after execution, change restore it.
The
subprocess
module is a very good solution.It has also arguments for modifying environment variables, redirecting input/output to the calling program, etc.
Try to
os.chdir(path)
before invoking the command.From here:
EDIT
This will change the current working dir, you can get the current working by:
If you want to save it and restore it later, if you need to do some work in the original working dir.
EDIT 2
In any case you should probably move to
subprocess
(doc) as suggested here. If you usesubprocess
'sPopen
you have the choice of providingcwd
parameter to specify the working directory for the subprocess: read this.