Python calls an external program

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 under running command, given by the Windows Env Ironment variable COMSPEC : on command.com Systems (Windows-cmd.exe, 98 and ME) this is always 0 ; Systems (Windows NT, and XP) This is the exit status of the command 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 () 17.1.4.3 with Subprocess.call (). 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:2     retcode = Call ("myCMD" + "Myarg", shell=true) 3     if Retcode < 0:4         print >>sys.stderr, "Child W  As terminated by signal ",-retcode5     else:6         print >>sys.stderr," Child returned ", Retcode7 except OSError as E:8     print >>sys.stderr, "Execution failed:", E
Example Demo:

Open Notepad:

1 Import os2 Os.system (' Notepad ')

Or

1 Import subprocess2 subprocess.call (' Notepad ')

Let's look at the following code:

1 Import os2 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 subprocess2 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 Import os2 Os.system (R ' "D:\Program Files (x86) \netease\cloudmusic\cloudmusic.exe" "Notepad" ') 3 or 4 Os.system ("D:\ Program Files (x86) \netease\cloudmusic\cloudmusic.exe "" Notepad ") 5 or 6 Os.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 an external program

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.