Python's subprocess module, sys module

Source: Internet
Author: User

One, subprocess module

#Import OS#os.system (' tasklist ') #类似cmd输入系统命令" "The purpose of subprocess is to start a new process and communicate with it. Only one class is defined in the Subprocess module: Popen. You can use Popen to create processes and make complex interactions with processes. " "ImportSubprocess#can replace Os.systemImport Time#stdout command correctly outputs results, pipes shared between pipe process and process, Shell=true represents invoke command interpreterobj=subprocess. Popen ('taskliasdfsadfst',#You can enter system commandsShell=True, stdout=subprocess. PIPE,#such as input tasklistStderr=subprocess. PIPE#such as input taskliasdfsadfst)#print (obj)Stdout_res=obj.stdout.read ()#from the son to put the right results into the pipeline, the father and other son put the correct results into the pipeline, the father from the correct pipe reading resultsPrint(Stdout_res.decode ('GBK'))##windows默认的是gbk编码的, so the decoding also needs to use GBKPrint(stdout_res)#Stderr_res1=obj.stderr.read () ##Stderr_res2=obj.stderr.read ()#Stderr_res3=obj.stderr.read ()#Print (Stderr_res1.decode (' GBK ')) #windows默认的是gbk编码的, so the decoding also uses GBK#print Result: ' Taskliasdfsadfst ' is not an internal or external command, nor is it a running program#or batch file. #print (stderr_res1) #第一次从管道中取走, followed by empty#print (stderr_res2)#print (STDERR_RES3)#Import Time#Time.sleep (+)

Second, sys module

ImportSYS#Sys.path: Gets a collection of strings for the specified module search path, which can be placed in a given path, and can be found correctly when import is in the program. #SYS.ARGV # used to receive the parameters followed by the Python interpreter execution py file#For example: Python cp.py argv1 argv2 arg3#sys.argv=[' cp.py ', ' argv1 ', ' argv2 ', ' argv3 ']ImportSYSPrint(sys.argv[0])Print(sys.argv[1])#sys.argv command line argument list, the first element is the path of the program itself#sys.exit (N) exit program, Exit normally (0)#Sys.version get version information for Python interpreter#Sys.maxint the largest int value#Sys.path Returns the search path for the module, using the value of the PYTHONPATH environment variable when initializing#Sys.platform returns the operating system platform name

SYS implements a copy of the file

" "execution file: cp.py" "ImportSYSPrint(SYS.ARGV)------------[]#src_file=input (' Please enter source file path: ')SRC_FILE=SYS.ARGV[1]#get the source file path through cmd#dst_file=input (' Please enter the path to the destination file: ')DST_FILE=SYS.ARGV[2]#get the target part path through CMDWith open (Src_file,'RB') as Read_f, open (Dst_file,'WB') as Write_f: forLineinchRead_f:write_f.write (line)

CMD Run content:

Python's subprocess module, sys module

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.