TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to PYTHON

Python subprocess Examples: subprocess.run

Use 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 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.

Subprocess: Python.org

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 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
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.

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...
A summary. Python has support for launching external processes at the level of the operating system. This is commonly needed, and commonly useful.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf