Python script execution method

Calling Python shell script commonly used functions have os.system, os.popen () and subprocess.Popen ()

  1. os.system method
    syntax: os.system (cmd)
    during the execution of os.system () in the main carried out: fork () out of a sub-process; the child process calls exec () command is executed.
    example 1:
    >>> import os
    >>> os.system('dir D:\Python')

    Python script execution method
    Succeeds, the return 0.
    Example 2:

    import os
    res = os.system('ping www.baidu.com -n 3')
    if res == 0:
    print('成功')
    else:
    print('失败了')

    The results:
    Python script execution method
    Note:
    1) .os.system () instruction execution cmd, 0 returns the result indicates success, returns a failure.
    2) .os.system () can not obtain the content cmd output.
    3) .os.system () system calls the external command, the command returns the result code, can not get command output.

  2. os.popen method
    Syntax: popen (cmd, mode = ' r', buffering = -1)
    parameters:
    cmd:要执行的命令。
    mode:打开文件的模式,默认为'r',用法与open()相同。
    buffering:(可选参数),0表无缓冲;1表行缓冲;-1表默认缓冲值;

    os.popen, () method opens a pipeline, the result is a conduit connected to the object file. Execute successfully returned the file object read returns the result, does not return a status code. It failed, returning an error message execution. And returns the result can be given to a variable, to facilitate processing program.
    Example 1: The
    contents of the file: D: \ Python \ test.py

    print('popen()测试')

    Contents of the file: D: \ Python \ test2.py

    res = os.popen(r'D:\Python\test.py', mode='r')
    res1 = res.read()       #返回结果赋于一变量,便于程序的处理
    print(res1)   
    print(type(res1))     #返回str
    res.close()

    python D: \ Python \ test2.py The results:
    Python script execution method

  3. subprocess module
    os.system, os.popen implemented functions can be implemented in subprocess module is recommended. And subprocess module can also call an external system command to create a new child process. subprocess execution time does not make the main system commands to perform the process, but the process will open up the main fork () a child process to execute, and will not affect the operation of the main process.
    subprocess module main call (), Popen () function.
    3.1 subprocess.Popen () function
    syntax:
    subprocess.Popen(args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags)  

    args: an external system command to invoke.
    bufsize: The default value is 0, no buffer; 1 represents the line buffer; other positive number indicates the size of the buffers used; -1 indicates a negative system default buffer size.
    stdin, stdout, stderr: represent standard input, standard output and standard error. The values are PIPE, file descriptors, etc. None. The default value is None, expressed inherited from the parent process.
    shell: Linux the parameter value is False, os.execvp executed by calling the corresponding program on Linux. True, a direct call on a Linux system shell to execute the program. Windows shell parameter set to true, the program will be executed through the shell.
    cwd: Set the child process current directory.
    env: env environment variable is the dictionary type, specify the child process. The default value is None, environment variables represent the child process inherited from the parent process.
    subprocess.PIPE: When creating Popen objects, subprocess.PIPE can initialize stdin, stdout or stderr parameters, represents the standard sub-process communication flow. The results thrown into the pipeline (shared memory space for sharing between processes).
    example 1:

    cmd = 'ipconfig | findstr "192.168.1.128"'
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)    #将正确的执行结果丢到管道中。
    res = p.stdout.read().decode('gbk')    #主进程去管道里获取正确stdout的结果,并将获取的结果的bytes类型转换。
    print(res)
    print(type(res))

    Python script execution method
    subprocess.Popen () method os.popen, () is compared, os.popen, () output is obtained directly readable, and outputs the result subprocess.Popen () is acquired in binary form and will newline .
    3.2 subprocess.call () function
    call to an external system command, and returns the result code execution. Function is similar to os.system (cmd). 0 indicates a successful execution, returns 1 for a failure.
    Example 2:

    import subprocess
    res = subprocess.call('ping www.baidu.com', shell=True)
    print('>>> ping www.baidu.com')
    print('测试res:', res)

    result:
    Python script execution method

Guess you like

Origin blog.51cto.com/10874766/2459674