Python subprocess.check_output(): Set working directory

If you have code that uses subprocess.check_output() to call a command like

subprocess_check_output_set_cwd.py
subprocess.check_output("ls .", shell=True)

you can use the cwd=... argument of subprocess.check_output() to define the working directory. Example:

example.py
subprocess.check_output("ls .", cwd="../", shell=True)

cwd means change working directory and is interpreted relative to the current working directory. However, you can also use absolute paths:

example.py
subprocess.check_output("ls .", cwd="/etc/", shell=True)

 


Check out similar posts by category: Python