Victor Victor - 1 year ago 79
Python Question

Command line execution in different folder

I'm calling a command line program in python using the


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.

Answer Source

The subprocess module is a very good solution.

import subprocess
p = subprocess.Popen([command, argument1,...], cwd=working_directory)

It has also arguments for modifying environment variables, redirecting input/output to the calling program, etc.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download