Python calls external programs--os.system () and Subprocess.call ()

Source: Internet
Author: User

calling other programs through the Os.system and Subprocess.call () functions Pre-knowledge: cmd to open and close programs

CMD to open the program

A. Open the system's own program

The path of the program that comes with the system is generally included in the environment variable, just enter the program name directly in the cmd window .

Take Notepad as an example, enter Notepad directly in the CMD window and return to open.

B. Open a program that you installed without adding environment variables

Take NetEase cloud Music as an example, in the cmd window need to enter the complete program path "D:\Program Files (x86) \netease\cloudmusic\cloudmusic.exe".

Note that double quotation marks are required .

If the path of NetEase cloud music is added to the environment variable, then enter Cloudmusic in the cmd window and return to run.

Close the program in CMD

To close a program in CMD, you can use the taskkill command with the following syntax:

taskkill/f/t/im Process name

Note that this only requires the process name of the program , not the full pathname.

Still take NetEase cloud music as an example, enter taskkill/f/t/im cloudmusic.exe after entering the cmd window and enter to turn off NetEase cloud music.

As shown in the following:

A.os.system Method

os.system(command) link Https://docs.python.org/2/library/os.html#os.system

Execute the command (a string) in a subshell. This was implemented by calling the standard C function system() , and have the same limitations. Changes sys.stdin to, etc. is not reflected in the environment of the executed command.

On Unix, the return value was the exit status of the process encoded in the format specified for wait() . Note that POSIX does isn't specify system() the meaning of the return value of the C function, so the return value of the Pytho n function is system-dependent.

On Windows, the return value is this returned by the system shell through running  command , given by the Win dows Environment Variable< Span class= "Pre" >comspec : On command.com  systems ( Windows 98 and ME) This is Always 0 ; On cmd.exe  systems (Windows NT, + and XP) This is the exit status of the COM Mand run; On systems using a non-native shell, consult your shell documentation.

The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that mod Ule is preferable to using the this function. See the replacing older Functions and the subprocess Module section in the subprocess documentation for some helpful recipes.

Availability:unix, Windows.

The system () function in the OS module makes it easy to run other programs or scripts. Its function prototypes are:
Os.system (command)
command to execute commands similar to those entered in the Windows CMD window.

If you want to pass parameters to a program or script, you can use a space-delimited program and multiple parameters.

B. Replace Os.system () with Subprocess.call ( )17.1.4.3. Replacing os.system()     链接 https://docs.python.org/2/library/subprocess.html#replacing-os-system
1 status = Os.system ("mycmd""  myarg")2  #  becomes3 status = Subprocess.call ("mycmd" " Myarg ", Shell=true)

Notes:

    • Calling the program through the shell was usually not required.

A more realistic example would look like this:

1 Try:2Retcode = Call ("myCMD"+"Myarg", shell=True)3     ifRetcode <0:4         Print>>sys.stderr,"Child is terminated by signal", -Retcode5     Else:6         Print>>sys.stderr,"Child returned", Retcode7 exceptOSError as E:8     Print>>sys.stderr,"Execution failed:"E
Example Demo:

Open Notepad:

1 Import OS 2 os.system ('notepad')

Or

1 Import subprocess 2 subprocess.call ('notepad')

Let's look at the following code:

1 Import OS 2 os.system (r'"D:\Program Files (x86) \netease\cloudmusic\cloudmusic.exe"' )

This code will start the NetEase cloud music, the effect and we enter "D:\Program Files (x86) \netease\cloudmusic\cloudmusic.exe" effect in the cmd window. Note that the string contains spaces, so there is R '.

The following code can also implement the same functionality:

1 Import subprocess 2 subprocess.call ("D:\Program Files (x86) \netease\cloudmusic\cloudmusic.exe")

The difference from the above code is only the R 'in parentheses.

So far everything is OK, let's look at the following code and try to open two programs at the same time:

1 ImportOS2Os.system (R'" D:\Program Files (x86) \netease\cloudmusic\cloudmusic.exe" "Notepad"')3 or4Os.system ("D:\Program Files (x86) \netease\cloudmusic\cloudmusic.exe""Notepad")5 or6Os.system (""D:\Program Files (x86) \netease\cloudmusic\cloudmusic.exe""Notepad"")

None of the above attempts will succeed.

The Subprocess.call () function is also not implemented.

This issue was submitted as early as 07, please refer to http://bugs.python.org/issue1524

The difference between Os.system () and Subprocess.call () is supplemented later.





Python calls external programs--os.system () and Subprocess.call ()

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.