Python subprocess Examples: subprocess.runUse the subprocess module and the subprocess.run method to invoke programs.
Subprocess. A process is an external program that executes on the operating system. It does something important (unless it does not).
Subprocess, notes. We can invoke subprocess.run after importing the subprocess module. Usually we invoke external EXE files. We can specify arguments.
A simple example.
Here we are on Windows and we want to invoke notepad.exe for an unknown reason. We can simply pass the string "notepad.exe" to subprocess.run.
Tip: To handle separators in paths better, we can use raw string literals with the "r" prefix.String Literals
Python program that uses subprocess
# Launch windows application.
# Launch windows application with argument.
# ... The window may be below the previous one.
We can use external programs, like the 7-Zip compression software, to compress files. We invoke an external binary file.
Here: We create a Windows command line that invokes the 7za.exe program. Please download 7za.exe from the 7-Zip website.7-Zip: Official Website
Source: The source string is used to build up the command line. This is the uncompressed file. You will need to change this.
Target: The target is another location on the disk. The compression version is written there.
Python program that uses subprocess, 7-Zip
exe = "C:\\7za.exe"
source = "C:\profiles\strong.bin"
target = "C:\profiles\strong.7z"
subprocess.call(exe + " a -t7z \"" + target + "\" \"" + source + "\" -mx=9")
7-Zip (A) 9.07 beta Copyright (c) 1999-2009 Igor Pavlov 2009-08-29
Creating archive C:\profiles\strong.7z
Everything is Ok
Notes, above program.
We use the subprocess module to call the executable. The subprocess.call method is an easy to way to invoke an external program. It works on all platforms.Subprocess: Python.org
This example uses subprocess to launch a more powerful compressor, PAQ. A PAQ executable is available in downloadable archives on the Internet.
Here: We use a PAQ8 implementation. The same command compresses, and expands, a file.
Raw: Please notice how the raw string syntax is used for paths—this should be used for Windows-style paths.
Python program that calls PAQ compressor
# This program handles compressed (fp8) files and non-compressed ones.
# ... It decompresses or compresses.
exe = r"C:\fp8_v2.exe"
source = r"C:\profiles\file.bin"
subprocess.call(exe + " " + source)
Creating archive C:\profiles\file.bin.fp8 with 1 file(s)...
File list (18 bytes)
Compressed from 18 to 22 bytes.
1/1 Filename: C:/profiles/file.bin (3836648 bytes)
0 | default | 28016 bytes [0 - 28015]
1 | jpeg | 8917 bytes [28016 - 36932]
2 | default | 3799715 bytes [36933 - 3836647]
Compressed from 3836648 to 114930 bytes.
Total 3836648 bytes compressed to 114958 bytes.
Time 44.26 sec, used 180892539 bytes of memory
Close this window or press ENTER to continue...
A summary. Python has support for launching external processes at the level of the operating system. This is commonly needed, and commonly useful.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to email@example.com.