Let's start with a simple example and run the Hello World! example in the same way as before, passing the command though the shell:
>>> import subprocess
>>> subprocess.Popen('echo "Hello world!"', shell=True)
Hello world!
As you can see, this prints Hello world! to the standard output as before but the interactive console also displays that we have created an instance of the subprocess.Popen class.
If you save this as process_test.py and execute it on the command line you get the same result:
$ python process_test.py
Hello world!
So far so good.
You might we wondering which shell is being used. On Unix, the default shell is /bin/sh. On Windows, the default shell is specified by the COMSPEC environment variable. When you specify shell=True you can customise the shell to use with the executable argument.
>>> subprocess.Popen('echo "Hello world!"', shell=True, executable="/bin/bash")
Hello world!
This example works the same as before but if you were using some shell-specific features you would notice the differences.
Let's explore some other features of using the shell:
Variable expansion:
>>> subprocess.Popen('echo $PWD', shell=True)
/home/james/Desktop
Pipes and redirects:
subprocess.Popen('echo "Hello world!" | tr a-z A-Z 2> errors.txt', shell=True)
>>> HELLO WORLD!
The errors.txt file will be empty because there weren't any errors. Interstingly on my computer, the Popen instance is displayed before the HELLO WORLD! message is printed to the standard output this time. Pipes and redirects clearly work anyway.
Here documents:
>>> subprocess.Popen("""
... cat << EOF > new.txt
... Hello World!
... EOF
... """, shell=True)
The new.txt file now exists with the content Hello World!.
Unsurprisingly the features that work with a command passed to the shell via the command line also work when passed to the shell from Python, including shell commands.
Comments
Post a Comment
https://gengwg.blogspot.com/