Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Combination and Application of python and shell

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

The last 0 of the function module system () is the return value of this command, and a value of 0 indicates that the command was executed successfully. The results of execution cannot be saved using system.

Example: > import os > os.system ('ls') blog.tar djangoblog.log managerblog.sh test.txtdata ENV nginx-1.15.11 wget-log0popen ()

Hint: python3 is obsolete

Example:

> import os > os.popen ('ls') > > os.popen (' ls'). Read () 'blog.tar\ ndata\ ndb.sql\ ndead.letter\ nDjangoBlog\ ndjangoblog.log\ nfile1\ nget-pip.py\ nindex.html\ nmanagerblog.sh\ nnginx-1.15.11\ nPython-3.5.2\ nstartDjangoBlog.sh\ nstart_uwsgi.ini\ ntest.txt\ nwget-log\ nzrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war\ n' > os. Popen ('ls'). Read (). Split ('\ n') ['blog.tar' 'data', 'db.sql',' dead.letter', 'DjangoBlog',' djangoblog.log', 'ENV',' file1', 'get-pip.py',' index.html', 'managerblog.sh',' nginx-1.15.11', 'Python-3.5.2',' startDjangoBlog.sh', 'start_uwsgi.ini',' test.txt', 'wget-log' 'zrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war',']

Gets the result of the execution of the command, but does not have the execution state of the command, so that the obtained results can be saved and placed in the list.

Subprocess can easily get the output of the command (including standard and error output) and execution status bits. Commands.getoutput ('ls') this method returns only the execution result and result does not return the status.

Hint: the subprocess module has replaced commands

Example:

1. Getstatusoutput > import subprocess > status,result=subprocess.getstatusoutput ('ls') > > status0 > result'blog.tar\ ndata\ ndb.sql\ ndead.letter\ nDjangoBlog\ ndjangoblog.log\ nENV\ nget-pip.py\ nindex.html\ nmanagerblog.sh\ nnginx-1.15.11\ nPython-3.5.2\ nstartDjangoBlog.sh\ nstart_uwsgi.ini\ ntest.txt\ nwget-log\ nzrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war' > > result.split ('\ n') ['blog.tar',' data' 'db.sql', 'dead.letter',' DjangoBlog', 'djangoblog.log',' ENV', 'file1',' get-pip.py', 'index.html',' managerblog.sh', 'nginx-1.15.11',' Python-3.5.2', 'startDjangoBlog.sh',' start_uwsgi.ini', 'test.txt',' wget-log', 'zrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war']

2 、 getoutput

> print (subprocess.getoutput ('ls'). Split ('\ n')) ['blog.tar',' data', 'db.sql',' dead.letter', 'DjangoBlog',' djangoblog.log', 'ENV',' file1', 'get-pip.py',' index.html', 'managerblog.sh',' nginx-1.15.11', 'Python-3.5.2',' startDjangoBlog.sh', 'start_uwsgi.ini' 'test.txt',' wget-log', 'zrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war']

3 、 call

Execute the command and return the status code (0 for normal execution and 1 for error)

> subprocess.call ('ls') blog.tar djangoblog.log managerblog.sh test.txtdata ENV nginx-1.15.11 wget-logdb.sql file1 Python-3.5.2 zrlog-2.1.3-b5f0d63-release.war?attname=ROOT.wardead.letter get-pip.py startDjangoBlog.shDjangoBlog index.html start_uwsgi.ini0

4 、 check_call

Execute the command, and return the status code 0 if the execution is successful, otherwise an exception is thrown

> subprocess.check_call ('ls') blog.tar djangoblog.log managerblog.sh test.txtdata ENV nginx-1.15.11 wget-logdb.sql file1 Python-3.5.2 zrlog-2.1.3-b5f0d63-release.war?attname=ROOT.wardead.letter get-pip.py startDjangoBlog.shDjangoBlog index.html start_uwsgi.ini0 > subprocess.check_call (' ls luojun') Traceback (most recent call last): File ", line 1 In File "/ usr/local/python3/lib/python3.5/subprocess.py", line 576, in check_call retcode = call (* popenargs, * * kwargs) File "/ usr/local/python3/lib/python3.5/subprocess.py", line 557, in call with Popen (* popenargs, * * kwargs) as p: File "/ usr/local/python3/lib/python3.5/subprocess.py", line 947, in _ _ init__ restore_signals Start_new_session) File "/ usr/local/python3/lib/python3.5/subprocess.py", line 1551, in _ execute_child raise child_exception_type (errno_num, err_msg) FileNotFoundError: [Errno 2] No such file or directory:'ls luojun'

5 、 check_output

Execute the command, and return the execution result if the execution is successful, otherwise an exception is thrown

> > subprocess.check_output ('ls') b'blog.tar\ ndata\ ndb.sql\ ndead.letter\ nDjangoBlog\ ndjangoblog.log\ nENV\ nfile1\ nget-pip.py\ nindex.html\ nmanagerblog.sh\ nnginx-1.15.11\ nPython-3.5.2\ nstartDjangoBlog.sh\ ntest.txt\ nwget-log\ nzrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war\ n' > subprocess.check_output ('ls luojun') Traceback (most recent call last): File " Line 1, in File "/ usr/local/python3/lib/python3.5/subprocess.py", line 626, in check_output * * kwargs). Stdout File "/ usr/local/python3/lib/python3.5/subprocess.py", line 693, in run with Popen (* popenargs, * * kwargs) as process: File "/ usr/local/python3/lib/python3.5/subprocess.py", line 947, in _ init__ restore_signals Start_new_session) File "/ usr/local/python3/lib/python3.5/subprocess.py", line 1551, in _ execute_child raise child_exception_type (errno_num, err_msg) FileNotFoundError: [Errno 2] No such file or directory:'ls luojun'

6. Subprocess.Popen (…)

Used to execute complex system commands

Parameter comment

Args shell command, which can be a string or sequence type (such as: list, tuple) bufsize specified buffer. 0 No buffer, 1 line buffer, other buffer size, negative system buffer stdin, stdout, stderr indicate that the standard input, output and error handle of the program preexec_fn is valid only on the Unix platform and is used to specify an executable object (callable object). It will be called close_sfs before the child process runs on the windows platform, if close_fds is set to True The newly created child process will not inherit the input, output, and error pipes of the parent process. Therefore, close_fds cannot be set to True to redirect the standard input, output, and error (stdin, stdout, stderr) of the child process at the same time. Shell as above cwd is used to set the current directory of the child process env is used to specify the environment variables of the child process. If env = None, the environment variables of the child process are inherited from the parent process. The newline characters of different universal_newlines systems are different. True-> agrees to use\ nstartupinfo only under windows and will be passed to the underlying CreateProcess () function, which is used to set some properties of the child process, such as the appearance of the main window, the priority of the process, etc. Createionflags calls shell script in python to write a script Pass in two arguments, [root@VM_0_2_centos test] # vim test.sh [root @ VM_0_2_centos test] # cat test.shend.shend.shedularbinapash bashecho "this is my test shell ${1} ${2}" exit 0 [root@VM_0_2_centos test] # chmod + x test.sh [root @ VM_0_2_centos test] # / test/test.sh jluo jluocc.cnthis is my test shell jluo jluocc.cn invokes the shell script in the python script, passing in the parameters Note that there should be a space [root@VM_0_2_centos test] # vim mytest.py [root @ VM_0_2_centos test] # cat mytest.py #! / usr/bin/env python3import osimport sysif (len (sys.argv)) before and after the parameter

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report