- How To Run Shell Command And Get Output In Python
- 1. Run Shell Command Use subprocess Module.
- 2. Run Shell Command Use os Module system And popen Function.
- 2.1 os.system(command).
- 2.2 os.popen(command, [mode, [bufsize]]).
- 3. Other Python os, shutil Module Functions Example.
- 4. Python System Environment Variable.
- 5. The os.path Module.
- 6. Run Shell Command In Python Examples.
- 6.1 The following is a simple script case for copying files.
- 6.2 The following example renames all files in a folder to 10001 ~ 10999.
- 7. File Redirection.
- Execute Shell Command and Get Output in Python
- Execute CMD Commands From a Python Script and Get Output Using os.system()
- Execute CMD Commands From a Python Script and Get Output Using the Subprocess Module
How To Run Shell Command And Get Output In Python
In the early python version, the os module’s system or popen function is usually used to execute operating system commands. However, recently python has gradually abandoned these functions officially. It recommends using the built-in subprocess module to execute operating system-related commands. This article will tell you how to use the subprocess module and os module’s system, popen function to run a shell command.
1. Run Shell Command Use subprocess Module.
# the child process will print it's standard output to a pipe line, the pipe line connect the child process and it's parent process. >>> child = subprocess.Popen(['ls','-l'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
>>> import subprocess >>> child = subprocess.Popen(['java','-version'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) >>> print(child.stdout.read()) b'openjdk version "1.8.0_212"\nOpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_212-b03)\nOpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.212-b03, mixed mode)\n'
2. Run Shell Command Use os Module system And popen Function.
2.1 os.system(command).
- Run operating system commands and display the results directly on the standard output device( ie: screen console).
- But the function’s return value is 0 or -1, and the data displayed on the screen cannot be obtained in the source code.
- The command parameter is the command string to be executed.
>>> import os >>> ret = os.system('java -version') openjdk version "1.8.0_212" OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_212-b03) OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.212-b03, mixed mode) >>> ret 0
2.2 os.popen(command, [mode, [bufsize]]).
- Start a child process to execute the command specified by the command parameter, and establish a pipeline between the parent process and the child process for communication between the parent and child processes.
- This method returns a file object, which can be read or written, depending on the parameter mode‘s value.
- If the mode parameter’s value is ‘r’, then the file is read-only. If the mode parameter’s value is ‘w’, then the file is write-only and it will throw an error when you want to get the file object’s content using it’s read() method.
- In short, popen method can run operating system commands and can return the result of the command through the result file object’s read() method.
- Below is an example.
>>> import os # invoke os.popen method to run a shell command. >>> ret = os.popen('ifconfig') # get the shell command execution result through the returned file object's read() method. >>> ret.read() 'lo0: flags=8049 mtu 16384\n\toptions=1203\n\tinet 127.0.0.1 netmask 0xff000000 \n\tinet6 ::1 prefixlen 128 \n\tinet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 \n\tnd6 options=201\ngif0: flags=8010 mtu 1280\nstf0: flags=0<> mtu 1280\nen0: flags=8863 mtu 1500\n\toptions=50b\n\tether 10:dd:b1:9b:b0:b9 \n\tnd6 options=201\n\tmedia: autoselect (none)\n\tstatus: inactive\nen1: flags=8863 mtu 1500\n\toptions=400\n\tether 4c:8d:79:e1:bc:e2 \n\tinet6 fe80::82a:736f:f606:31f0%en1 prefixlen 64 secured scopeid 0x5 \n\tinet 192.168.31.31 netmask 0xffffff00 broadcast 192.168.31.255\n\tnd6 options=201\n\tmedia: autoselect\n\tstatus: active\nen3: flags=8963 mtu 1500\n\toptions=460\n\tether 82:0a:57:17:dd:80 \n\tmedia: autoselect \n\tstatus: inactive\nfw0: flags=8863 mtu 4078\n\tlladdr 10:dd:b1:ff:fe:5c:5f:76 \n\tnd6 options=201\n\tmedia: autoselect \n\tstatus: inactive\nbridge0: flags=8863 mtu 1500\n\toptions=63\n\tether 82:0a:57:17:dd:80 \n\tConfiguration:\n\t\tid 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0\n\t\tmaxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200\n\t\troot id 0:0:0:0:0:0 priority 0 ifcost 0 port 0\n\t\tipfilter disabled flags 0x0\n\tmember: en3 flags=3\n\t ifmaxaddr 0 port 6 priority 0 path cost 0\n\tnd6 options=201\n\tmedia: \n\tstatus: inactive\np2p0: flags=8843 mtu 2304\n\toptions=400\n\tether 0e:8d:79:e1:bc:e2 \n\tmedia: autoselect\n\tstatus: inactive\nawdl0: flags=8943 mtu 1484\n\toptions=400\n\tether 02:c2:69:6d:b3:5e \n\tinet6 fe80::c2:69ff:fe6d:b35e%awdl0 prefixlen 64 scopeid 0xa \n\tnd6 options=201\n\tmedia: autoselect\n\tstatus: active\nutun0: flags=8051 mtu 1380\n\tinet6 fe80::a4be:4aed:be5b:253%utun0 prefixlen 64 scopeid 0xb \n\tnd6 options=201\nutun1: flags=8051 mtu 2000\n\tinet6 fe80::cfcf:1568:4d94:3b0a%utun1 prefixlen 64 scopeid 0xc \n\tnd6 options=201\n'
3. Other Python os, shutil Module Functions Example.
- os.listdir(dirname): This function returns a list of strings containing all file names under the directory, but excluding “.” and “..”.
>>> import os >>> >>> os.listdir('.\\') ['.anaconda', '.cache', '.conda', '.condarc', '.config', '.continuum', '.eclipse', '.FAILED_FILE_CACHED_DIR', '.gitconfig', '.lemminx', '.m2', '.matplotlib', '.p2', '.VirtualBox']
>>> import os >>> >>> os.getcwd() 'C:\\Users\\zhaosong'
>>>import os >>> >>>os.getcwd() 'D:\\' >>> >>>os.chdir('c:\\') >>> >>>os.getcwd() “c:\\”
os.S_ISUID, os.S_ISGID, os.S_ENFMT, os.S_ISVTX, os.S_IREAD, os.S_IWRITE, os.S_IEXEC, os.S_IRWXU, os.S_IRUSR, os.S_IWUSR, os.S_IXUSR , os.S_IRWXG, os.S_IRGRP, os.S_IWGRP, os.S_IXGRP, os.S_IRWXO, os.S_IROTH, os.S_IWOTH, os.S_IXOTH R stands for read, W for write, and X for execute permission. USR represents the user, GRP represents the group, OTH represents others.
4. Python System Environment Variable.
- The python environment variable is stored in the os.environ dictionary. It can be modified by using the ordinary dictionary method. It will be inherited automatically when starting other programs with the system.
- The value of a python environment variable can only be a string. Unlike the shell, python does not have the concept of exporting environment variables.
- The below example will add a python environment variable.
5. The os.path Module.
- This module contains many functions for pathname processing. In the shell, pathname processing seems not very important, but it is often used in Python. The two most commonly used are separating and merging directory names and file names.
- os.path.split(path) -> (dirname, basename): This function will separate a path into two parts.
>>> import os >>> >>> os.path.split("/foo/bar.dat") ('/foo', 'bar.dat')
>>> import os >>> >>> os.path.join("/foo","bar.dat") '/foo\\bar.dat'
>>> import os >>> >>> os.path.splitext("/foo /bar.tar.bz2") ('/foo /bar.tar', '.bz2')
6. Run Shell Command In Python Examples.
6.1 The following is a simple script case for copying files.
- There is a directory of tens of thousands of files.
- I want to copy these files to other directories, but I do not want to copy the directory itself directly.
- I tried “cp src/ * dest/” and it reported an error that the command line was too long.
- So I write the below python source code to implement the task.
import sys,os.path,shutil # sys.argv[0] is the python program name. # sys.argv[1] is the source directory. # sys.argv[2] is the target directory. # loop in the source directory. for f in os.listdir(sys.argv[1]): # get the source file full path value. src_file_path = os.path.join(sys.argv[1],f) # copy the source file to the target directory shutil.copy(src_file_path,sys.argv[2])
6.2 The following example renames all files in a folder to 10001 ~ 10999.
import os.path,sys # get the files source folder. dirname=sys.argv[1] i = 10001 # loop all the files in the directory. for f in os.listdir(dirname): # get the source file. src=os.path.join(dirname,f) # check whether the file is a directory or not. if os.path.isdir(src): # if the file is a directory then continue. continue # otherwise rename the file name. os.rename(src,str(i)) i += 1
7. File Redirection.
- There is an existing py file new1.py.
- When you enter the command new1 > new.txt on the command line to output the running result of new1 to the file new.txt, this is called stream redirection
Execute Shell Command and Get Output in Python
- Execute CMD Commands From a Python Script and Get Output Using os.system()
- Execute CMD Commands From a Python Script and Get Output Using the Subprocess Module
In this article, we will learn how to execute cmd commands from a Python script with the help of os.system() . We will also learn how we can execute cmd commands from the script in an easier way with the help of the subprocess module in Python.
Execute CMD Commands From a Python Script and Get Output Using os.system()
We execute terminal commands in command prompt or any other terminal for different purposes. But, sometimes, running a particular command inside the script is necessary.
We will see how we can execute them directly inside the Python script. It is handy when we work with server configuration.
First, let us show you some commands that work in the terminal, such as dir , cd , or md .
Now, we will see how we can include the same ones in the Python script. To do that, we will import a module called os .
The os module will help us to interact with our operating system. The os module has extensive support for operating system tasks such as files and folder management.
Let’s jump into the code. The system() is a method that executes the command in the like shell, so if we give it any command, it will go ahead and execute them the way we would execute them in the terminal.
The system function can also execute a bunch of commands. It executes every command that you can run in the terminal.
We will use the try block, and inside this block, we will use the system() method, which will help us to interact with the operating system using the terminal. If the try block does not execute the specified command, we will go onto the except block.
Inside the system() method, we have to pass our commands, but the command type is cmd . For that, we use /k , and inside a single or double quotation, we have to type our command.
import os try: os.system('cmd /k "date"') except: print('Could not execute command')
Let’s run and see whether this gives us the exact output.
The current date is: 24/08/2022 Enter the new date: (dd-mm-yy)
We can see the output is the same as the command prompt gives.
There are lots of commands you can execute. You can open Notepad or Calculator, or you can see your system information and much more.
If you want to get the content of what the command returns, you can get it using the popen() function of the os module. Inside this function, we can pass the command and use the readlines() method to get its content.
We can use many methods to get clean data. It depends on you.
import os DATA=os.popen('help').readlines()[5].strip('\n') print(DATA)
We can use these commands anywhere, like class, loops, and function. This will properly work as it does without wrapping it in a function.
import os def CMD_Com(): DATA=os.popen('help').readlines()[5].strip('\n') print(DATA) CMD_Com()
CACLS Displays or modifies access control lists (ACLs) of files.
Execute CMD Commands From a Python Script and Get Output Using the Subprocess Module
Interacting with subprocesses is an essential skill to have. Using the os module is not recommended to execute a terminal command inside the Python script.
Using os.system() to execute the terminal command is a very simplified way of running a command in Python.
The os.system() has limited functionality; the proper way is to use a module called subprocess , which makes it not too challenging to execute terminal commands. Using the subprocess module, we can run all the operating system commands on which we are currently working.
This is how we can run all existing commands in an operating system, like opening Notepad or checking the current working directory, or any other operation we can execute using the subprocess module.
import subprocess # subprocess.Popen('notepad') # subprocess.Popen('systeminfo') subprocess.Popen("dir", shell=True)
Directory of C:\Users\Dell\Downloads\demo 24/08/2022 07:01 pm . 24/08/2022 07:01 pm .. 25/08/2022 01:47 am 460 demo.py 1 File(s) 460 bytes 2 Dir(s) 32,532,512,768 bytes free
You can learn more about the subprocess module from here.
Hello! I am Salman Bin Mehmood(Baum), a software developer and I help organizations, address complex problems. My expertise lies within back-end, data science and machine learning. I am a lifelong learner, currently working on metaverse, and enrolled in a course building an AI application with python. I love solving problems and developing bug-free software for people. I write content related to python and hot Technologies.