C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: To handle separators in paths better, we can use raw string literals with the "r" prefix.
String LiteralsPython program that uses subprocess
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")
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.
Subprocess: Python.orgSource: 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
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")
Output
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
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
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)
Output
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...