In Python, four methods are used to run other programs, and four are python.

Source: Internet
Author: User

In Python, four methods are used to run other programs, and four are python.
In Python, you can easily use the OS module to run other scripts or programs, so that you can directly use the functions provided by other scripts or programs in the script, you do not have to write code to implement this function again. To better control the running process, you can use the functions in the win32process module. To further control the process, you can use the ctype module to directly call the functions in kernel32.dll.


Method 1]Use the OS. system () function to run other programs

The system () function in the OS module can easily run other programs or scripts. The mode is as follows:

OS. system (command)

  • Command: the command to be executed. If you want to pass parameters to the script, you can use spaces to separate the program and multiple parameters.

Example:
>>> Import OS >>> OS. system ('notepad ') # Open the notepad program. 0 >>> OS. system ('notepad 1.txt ') # Open the 1.txt file. If it does not exist, create it. 0
Method 2] Use the ShellExecute Function to run other programs
In addition to the OS. system () function, you can use the ShellExecute () function in the win32api module to run other programs. The format is as follows:
ShellExecute (hwnd, op, file, args, dir, show)
  • Hwnd: handle of the parent window. If there is no parent window, it is 0.
  • Op: The operation to be run, which is open, print, or empty.
  • File: The program to run or the script to open
  • Args: the parameter to be passed to the program. It is null if the file is opened.
  • Dir: directory of program Initialization
  • Show: whether to display the window
Example:
>>> Import win32api >>> win32api. shellExecute (0, 'open', 'notepad.exe ', '','', 0) # background execution >>> win32api. shellExecute (0, 'open', 'notepad.exe ', '','', 1) # open at the front end> win32api. shellExecute (0, 'open', 'notepad.exe ', '1.txt', '', 1) # open a file >>> win32api. shellExecute (0, 'open', 'HTTP: // www.sohu.com ', '','', 1) # open the webpage >>> win32api. shellExecute (0, 'open', 'd: \ Opera.mp3 ', '','', 1) # play a video >>> win32api. shellExecute (0, 'open', 'd: \ hello. py ', '','', 1) # run the program
Using the ShellExecute Function is equivalent to double-clicking the file icon in the Resource Manager. The system opens the corresponding program to run.

NOTE: 

Win32api installation http://sourceforge.net/projects/pywin32/files/pywin32/ because of the 64 operating system, so download this: pywin32-216.win-amd64-py2.7



Method 3] Use the ShellExecute Function to run other programs

Creation process:
You can use the CreateProcess () function in the win32process module to create programs that run through scripts.

A process that runs the corresponding program. The function format is:

CreateProcess (appName, cmdLine, proAttr, threadAttr, InheritHandle, CreationFlags, newEnv, currentDir, Attr)

  • AppName Executable File Name
  • CmdLine command line parameters
  • ProcAttr process security attributes
  • ThreadAttr thread security attributes
  • InheritHandle inheritance flag
  • CreationFlags
  • Current Directory of the currentDir Process
  • Attributes of the Attr Creation Program

Example:
>>> Win32process. createProcess ('C: \ Windows \ notepad.exe ', '', None, None, 0, win32process. CREATE_NO_WINDOW, None, None, win32process. STARTUPINFO () (<PyHANDLE: 892>, <PyHANDLE: 644>, 21592,187 80) # The function returns the Process Handle, thread handle, process ID, and thread ID.


End Process:

You can use the win32process. TerminateProcess function to end the created process. The function format is as follows:

TerminateProcess (handle, exitCode)
  • Handle Process handle to be operated
  • ExitCode process exit code

Alternatively, use win32event. WaitForSingleObject to wait for the end of the created thread. The function format is as follows:

WaitForSingleObject (handle, milisecond)
  • Handle: Process handle to be operated
  • Milisecond: the waiting time. If it is-1, it will be waiting.

Example:
>>> Import win32process >>> handle = win32process. createProcess ('C: \ Windows \ notepad.exe ', '', None, None, 0, win32process. CREATE_NO_WINDOW, None, None, win32process. STARTUPINFO () # Open notepad and obtain its handle >>> win32process. terminateProcess (handle [0], 0) # terminate a process or >>> import win32event >>> handle = win32process. createProcess ('C: \ Windows \ notepad.exe ', '', None, None, 0, win32process. CREATE_NO_WINDOW, None, None, win32process. STARTUPINFO () # create a process to obtain the handle >>> win32event. waitForSingleObject (handle [0],-1) # Wait for process to end 0 # Return Value of process to end
Method 4] Use ctypes to call functions in kernel32.dll
The ctypes module allows Python to call functions in the dynamic link library.

The ctypes module provides Python with the function of calling functions in the dynamic link library. The ctypes module allows you to conveniently call a dynamic link library written in C language and PASS Parameters to it. The ctypes module defines the basic data types in the C language and can implement the structure and consortium in the C language. The ctypes module can work in Windows, Linux, Mac OS, and other operating systems, and basically implements cross-platform.

Example:

In Windows, call the MessageBoxA function in user32.dll.

>>> from ctypes import *>>> user32 = windll.LoadLibrary('user32.dll')>>> user32.MessageBoxA(0, str.encode('Ctypes is so smart!'), str.encode('Ctypes'), 0)1

The basic types in the ctype module are similar to those in the C language. The following is a comparison of several basic data types:

---------------------------------------

Ctypes Data Type C data type
---------------------------------------
C_char char
C_short short
C_int int
C_long long
C_float float
C_doule double

C_void_p void *

---------------------------------------

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.