Change Directory in Python Subprocess
Here's how to change directory when shelling out to a process in python.
I'll show two methods. I want to run each process in the run_path
, set to some valid path. This example is a process called pydeps
, but it could be anything, os.getcwd()
, what have you.
First: cwd
import subprocess
process = subprocess.Popen(f"pydeps --no-output --show-deps --max-bacon=0 .".split(), cwd=run_path, stdout=subprocess.PIPE)
Second: chdir
import os
import subprocess
os.chdir(run_path)
process = subprocess.Popen(f"pydeps --no-output --show-deps --max-bacon=0 .".split(), stdout=subprocess.PIPE)
os.chdir(service_path)
output, error = process.communicate()
Both should work, but depending on the command, output may differ. At least in my current circumstance it does, though I'm still unsure why.