Several methods for python to execute shell scripts

Preface: As a scripting language, Python sometimes needs to be used interactively with shell commands. Python provides many methods to call and execute shell scripts. This article makes a brief summary. The development environment for this article is Ubuntu 16.04.

1. os.system("command")

This is python's own method of executing shell commands, where the last 0 is the return value of this command, and 0 indicates that the command was executed successfully. But using system() cannot save the result of execution.

as follows:

  1. import them
  2. print(os.system("touch a.txt"))

Will return a 0, indicating that the execution was successful, and then create a new a.txt file under the current folder

Another example:

  1. import them
  2. print(os.system("ls -a"))

It also returns a 0, but there is no way to view the result of this command execution, that is, the system function does not return the result of shell command execution.

2. os.popen("command") method 

The above os.system() method cannot view the result returned by the shell command. The file read object returned by os.popen() can be read by the read() operation to see the output of the execution.

Note: os.popen() returns a file object f! ! !

as follows:

  1. import them
  2. f=os.popen( "ls -l" ) # returns a file object
  3. print (f.read()) # Read the returned content through the file's read()
  4. '''
  5. total 4
  6. -rw-rw-r-- 1 tengjian tengjian 0 11月 5 09:32 a.txt
  7. -rw-rw-r-- 1 tengjian tengjian 81 11月 5 09:32 python_shell.py
  8. '''

For those shell commands that do not return a pointer, I can still use the popen() method, as follows:

  1. import them
  2. f=os.popen( "touch b.txt" ) # create a file
  3. # f=os.popen("mkdir newdir") # create a new folder
  4. print (f.read()) # no return value

Summarize:

  • For shell commands with return values, it is recommended to use os.popen()
  • For shell commands with no return value, it is recommended to use os.system()

Of course, in addition to these two methods of the os module that comes with the python system, there are many other methods to implement shell command operations, a few of which are described below.

Third, through the subprocess module

The subprocess module is a module introduced by python since version 2.4, and it is also built into the system, so no additional installation is required. Mainly used to replace some old module methods, such as os.system, os.spawn*, os.popen*, commands.*, etc. The subprocess executes external instructions through the subprocess, and obtains the return information of the execution of the subprocess through the input/output/error pipeline.

3.1 Common methods:

(1) subprocess.call(): Execute the command and return the execution status. When the shell parameter is False, the command and command parameters need to be passed in through a list. When the shell is True, it can be passed directly through a string. Enter the command and the parameters required by the command

An example is as follows:

  1. import subprocess
  2. print (subprocess.call([ "ls" , "-l" ],shell= False )) # If the shell parameter is false, then the command and parameters are given in the form of a list
  3. '''
  4. total 8
  5. -rw-rw-r-- 1 tengjian tengjian 0 11月 5 09:32 a.txt
  6. -rw-rw-r-- 1 tengjian tengjian 0 11月 5 09:33 b.txt
  7. drwxrwxr-x 2 tengjian tengjian 4096 Nov 5 09:32 hahaha
  8. -rw-rw-r-- 1 tengjian tengjian 119 11月 5 10:22 python_shell.py
  9. 0
  10. '''

The last state is 0, indicating that the command was executed successfully.

  1. import subprocess
  2. a=subprocess.call([ "ls" , "-l" ],shell= False ) # If the shell parameter is false, then the command and parameters are given in the form of a list
  3. print(a)
  4. '''
  5. 0 At this time, only 0 is returned to indicate that the execution was successful
  6. '''

When the shell parameter is True:

  1. import subprocess
  2. print (subprocess.call( "ls -l" ,shell= True )) # The shell parameter is true, then the command and parameters are given in the form of strings
  3. '''
  4. total 8
  5. -rw-rw-r-- 1 tengjian tengjian 0 11月 5 09:32 a.txt
  6. -rw-rw-r-- 1 tengjian tengjian 0 11月 5 09:33 b.txt
  7. drwxrwxr-x 2 tengjian tengjian 4096 Nov 5 09:32 hahaha
  8. -rw-rw-r-- 1 tengjian tengjian 119 11月 5 10:22 python_shell.py
  9. 0
  10. '''

 

 

Of course, the same operation can be done for some shell commands without return values, as follows:
  1. import subprocess
  2. print (subprocess.call([ "touch" , "c.txt" ],shell= False )) # Returns 0 directly, indicating that the operation is successful
  3. print (subprocess.call([ "mkdir newdir1" ],shell= True )) # Returns 0 directly, indicating that the operation is successful
(2) subprocess.check_call(): The usage is similar to subprocess.call(), the difference is that when the return value is not 0, an exception is thrown directly, which will not be repeated here.

(3) subprocess.check_output(): The usage is similar to the above two methods, the difference is that if the return value is 0, the output result is directly returned, and if the return value is not 0, an exception is thrown directly. It should be noted that this method is only available in python3.x.

  1. import them
  2. import subprocess
  3. a=subprocess.check_output(["ls","-l"],shell=False)
  4. print (a) # Instead of returning 0 directly, it directly returns the content of the execution result
  5. '''
  6. b'total 8\n-rw-rw-r-- 1 tengjian tengjian 0 11\xe6\x9c\x88 5 09:32 a.txt\n-rw-rw-r-- 1 tengjian tengjian 0 11\xe6\x9c\x88 5 09:33 b.txt\n-rw-rw-r-- 1 tengjian tengjian 0 11\xe6\x9c\x88 5 10:25 c.txt\ndrwxrwxr-x 2 tengjian tengjian 4096 11\xe6\x9c\x88 5 09:32 hahaha\n-rw-rw-r-- 1 tengjian tengjian 181 11\xe6\x9c\x88 5 10:33 python_shell.py\n'
  7. '''

Note: There are more functions of subprocess, such as Popen class

A Popen class is defined in the subprocess module, which can be used to create processes and perform complex interactions with them

 

Fourth, through the sh library

First install the sh library: pip install sh

Python is a great scripting language, but sometimes using the standard os and subprocess libraries can be a little tricky.

The sh library provides a nice alternative.

sh library: http://amoffat.github.io/sh/

Libraries allow users to call arbitrary programs like normal functions, which is useful for automating workflows and tasks.

Its general working mode is as follows:

sh.command_name("parameter one", "parameter two", "parameter three")

  1. import sh
  2. sh.pwd()
  3. sh.mkdir( new_folder )
  4. sh.touch( new_file.txt )
  5. sh.whoami()
  6. sh.echo( This is great! )
  1. sh.ls( "-l" ) # Equivalent to ls -l
  2. print(sh.ls("-l"))
  3. sh.df( "-h" ) # Equivalent to df -h
  4. print(sh.df("-h"))
  5. # When there are multiple parameters, and the parameters can be assigned
  6. sh.du( "-h" , "-d 1" ) # Equivalent to du -h -d 1
  7. print(sh.du("-h","-d 1"))

 Note: The above may not have code hints when writing through sh.**, because there is no direct definition in the sh module like the above

These functions, such as pwd(), touch(), du(), etc., are implemented in other ways, so there is no intelligent sensitive prompt.

 

Tags: Several methods for python to execute shell scripts

python advanced python interacts with the shell os.system os.popen subprocess module sh module

Related: Several methods for python to execute shell scripts