Home
Map
subprocess Examples: subprocess.runUse the subprocess module and the subprocess.run method to invoke programs.
Python
This page was last reviewed on Feb 25, 2024.
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.
multiprocessing
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 Literal
import subprocess # Launch windows application. subprocess.run("notepad.exe") # Launch windows application with argument. # ... The window may be below the previous one. subprocess.run(r"notepad.exe C:\programs\file.txt")
Subprocess, 7-Zip. 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.
Info We use the subprocess module to call the executable. The subprocess.call method is an easy to way to invoke an external program.
Note The source string is used to build up the command line. This is the uncompressed file. You will need to change this.
Note 2 The target is another location on the disk. The compression version is written there.
import subprocess 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 Scanning Creating archive C:\profiles\strong.7z Compressing strong.bin Everything is Ok
Subprocess, PAQ. 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.
Note Please notice how the raw string syntax is used for paths—this should be used for Windows-style paths.
import subprocess # 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) Block segmentation: 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...
Summary. Python has support for launching external processes at the level of the operating system. This is commonly needed, and commonly useful.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Feb 25, 2024 (edit link).
Home
Changes
© 2007-2024 Sam Allen.