calling other programs through the Os.system and Subprocess.call () functions
Preliminary knowledge: Open and close programs in cmd
Open program in cmd
A. Open the system's own program
The path of the system with the program is generally added to the environment variables, just enter the program name directly in the cmd window.
In Notepad, for example, directly in the cmd window input Notepad after enter can be opened.
B. Open a program that you installed without adding environment variables
To NetEase cloud Music for example, in the cmd window needs to enter the complete program path "D:\Program Files (x86) \netease\cloudmusic\cloudmusic.exe".
Note that double quotes are required.
If the path of NetEase cloud music into the environment variables, then in the cmd window input cloudmusic back to run.
close the program in CMD
The taskkill command can be used to close a program in CMD, and the syntax is as follows:
taskkill/f/t/im Process Name
Note that only the process name of the program is required, not the full pathname.
Still take NetEase cloud music for example, in the cmd window input taskkill/f/t/im Cloudmusic.exe back to turn off NetEase cloud music.
As shown in the following illustration:
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 are implemented by calling the Standard C function System (), and has the same limitations. Changes to Sys.stdin, etc. are 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 This POSIX does not specify the meaning of the ' return value of ' the ' C system () function, so the ' return value of ' the Py Thon function is system-dependent.
On Windows, the ' return ' value is ' returned by the ' system shell after running command, given by the Windows environment Variablecomspec:on Command.com Systems (Windows, and ME) is always 0; On cmd.exe systems (Windows NT, 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; The using that module is preferable the to using this function. The replacing older functions with the subprocess Module section in the subprocessdocumentation for some helpful Es.
Availability:unix, Windows.
The system () function in the OS module makes it easy to run other programs or scripts. Its function prototype is:
Os.system (command)
command is similar to the command entered in the Windows CMD window for the commands to execute.
If you want to pass arguments to programs or scripts, you can use spaces to separate programs and multiple parameters. B. Replace Os.system () with Subprocess.call ( ) 17.1.4.3. Replacing Os.system () link Https://docs.python.org/2/library/subprocess.html#replacing-os-system
1 status = Os.system ("mycmd" + "Myarg")
2 # becomes
3 status = Subprocess.call ("mycmd" + "Myarg", Shell=true)
Notes:calling the through the shell is 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 is terminated by signal",-retcode
5 Else:
6 print >>sys.stderr, "child Returned ", Retcode
7 except OSError 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 NetEase cloud music, the effect and we enter in the cmd window "D:\Program Files (x86) \netease\cloudmusic\cloudmusic.exe" effect. Note that the string contains spaces, so there is an R '.
The following code can also achieve the same function:
1 Import subprocess
2 subprocess.call ("D:\Program Files (x86) \netease\cloudmusic\cloudmusic.exe")
The difference with the above code is only the R ' in parentheses.
So far everything is OK, so let's look at the code below and try to open two programs at the same time:
1 Import OS
2 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 these attempts will succeed.
The Subprocess.call () function cannot be implemented either.
This question has been submitted as early as 07, please refer to http://bugs.python.org/issue1524
Later the difference of the Os.system () and Subprocess.call () is supplemented.