First, you should pass a list to Popen
, not a string. Each element of the list is one fragment of the commands you want to run. In your case it should be:
proc = subprocess.Popen(['cd', 'path/to/folder', '&', 'command'])
Second, if your command
is a system command like cmd
. You need to tell Python to use the system shell.
proc = subprocess.Popen(['cd', 'path/to/folder', '&', 'command'], shell=True)
Third, it looks like you want to capture the output from the command. Right now anything that the command does will be written to screen as if you had run the command from the shell. To get Python to capture the outputs, we need to redirect them from writing to the screen back to Python. We do this using subprocess.PIPE
for the stdout
and stderr
streams.
proc = subprocess.Popen(['cd', 'path/to/folder', '&', 'command'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
Lastly, using Popen
returns a Popen
object. You don't get the results right away, because the system might be churning away behind the scenes. Using Popen
allows your code to keep running (Popen is non-blocking). To get the output, you need to run the communicate
method. This returns the output from the output and error streams.
out, err = proc.communicate()
Example:
Change directory and list contents.
proc = subprocess.Popen(['cd', 'Documents/Projects', '&', 'dir'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = proc.communicate()
print(out.decode('latin-1'))
# prints
Volume in drive C is OS
Volume Serial Number is DE6D-0D88
Directory of C:\Users\james\Documents\Projects
01/04/2020 01:50 PM <DIR> .
01/04/2020 01:50 PM <DIR> ..
11/15/2019 09:05 PM <DIR> .vscode
01/09/2020 11:15 PM <DIR> CondaDeps
01/03/2020 09:22 PM <DIR> Django
12/21/2019 10:52 PM <DIR> FontCluster
12/20/2019 03:56 PM 70 fontcluster.code-workwspace.code-workspace
11/08/2019 03:01 PM <DIR> ScholarCrawler
12/30/2019 10:48 AM 56 scholarcrawler.code-workspace
07/24/2019 09:56 PM <DIR> ThinkStats2
2 File(s) 126 bytes
8 Dir(s) 415,783,694,336 bytes free