This article mainly introduced the Python simple Realization control computer's method, has the certain reference value, now shares to everybody, has the need friend to refer to
This article mainly describes the Python simple implementation of computer control methods, involving Python based on the OS and WIN32API modules called system commands to operate the computer related implementation skills, the need for friends can refer to the following
The example in this paper describes the simple implementation of Python control computer methods. Share to everyone for your reference, as follows:
1. Some commands under Windows, cmd:
dir: List all current files
Time: Print the current date
Tree: Lists the sub-structures under the current directory
Enter a certain mode in cmd, exit can try the following command: Q, Exit (), CTRL + C, CTRL + Z
Run the program: Enter the program name directly inside the CMD. such as: Notepad, Calc
Press the TAB key to complete the name
In a folder, want to quickly open cmd: Hold down the SHIFT key, the right mouse button, you can see the command.
Want a file in cmd, but enter the name after the show file or command does not exist. The file directory can be added to the path environment.
Shutdown: shutdown-s-t +3600-c "Off!" "#3600为时间, which shuts down after 1 hours and displays on the screen," Shut down! ”
Cancel Shutdown command: shutdown-a
2. Python control cmd
2.1. os.system('xxx')
xxx is the command executed in CMD
2.2. subprocess.Popen('xxx',shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
XXX is the command that executes in CMD, the other does not change.
Example:
#-*-Coding:utf-8-*-import osos.system ("Ping www.baidu.com")
#-*-Coding:utf-8-*-import subprocessa=subprocess. Popen ("Ping www.baidu.com", Shell=true, stdout=subprocess. PIPE, Stderr=subprocess. STDOUT) B=a.stdout.readlines () for i in B: print I
os.system
is a step-by-step printout, and subprocess.Popen
The final result is returned at once.
Under the directory, build a file conf.txt. Enter ping www.baidu.com in the file
#-*-Coding:utf-8-*-import osimport time## chra = "Ping www.baidu.com" # Os.system (CHRA) # # import subprocess## a = SUBPR Ocess. Popen (Chra, Shell=true, stdout=subprocess. PIPE, Stderr=subprocess. STDOUT) # b = A.stdout.readlines () # for I in b:# print iwhile True: f = open (' Conf.txt ', ' r ') content = F.read ( ) Os.system (content) Time.sleep (5)
You will see the program run Ping once every 5 seconds. Change the contents of the Conf.txt in Dir, found that the program is no longer ping, but the file name of the print folder.
3. Python module Win32API
3.1, Win32API. Beep
Beep (Freq, dur) freq represents the frequency, and the Dur represents the duration.
#-*-Coding:utf-8-*-import Win32apiwin32api. Beep (6000,3000)
It lasts three seconds, hears the squeaking noise.
3.2, Win32API. MessageBox
MessageBox(hwnd, message , title , style , language )
A window will pop up
Hwnd:int the location from which to pop the window. Typically 0
Message: Window Contents
Title: Header Name
Style=win32con. Mb_ok:int,the style of the message box.
Language=win32api. Makelangid (Lang_neutral,sublang_default): Int,the language ID to use.
#-*-Coding:utf-8-*-import win32apiimport time#win32api. Beep (6000,3000) while True: f = open (' Conf.txt ', ' r ') content = F.read (). Split (' # ') if content[0]! = ' O ': Win32API. MessageBox (0, content[1], content[2]) time.sleep (5) #conf. TXT content: "1 # Hi, beautiful girl# how is you!"
pops up a window with a display named "How is you!" with the content "Hi, beautiful Girl".
3.3, Win32API. ShellExecute
int = ShellExecute (hwnd, OP, file, params, dir, bShow) execute the program
Hwnd:intint the location from which to pop the window. Typically 0
The op:string operator. The operation to perform. May is "open", "print", or None, which defaults to "open".
The address of the file:string file. The name of the file to open.
Params:string. can be empty. The parameters to pass, if the file name is contains an executable. Should is None for a document file.
Dir:string. can be empty. The initial directory for the application.
Bshow:int. 1 means open window; 0 means no open. Specifies whether the application was shown when it was opened. If the Lpszfile parameter specifies a document file, this parameter is zero.
#-*-Coding:utf-8-*-import Win32apiwin32api. ShellExecute (0, ' open ', R ' C:\Users\Administrator\Pictures\toutiao\1.jpg ', ', ', 1)
Running the program will open the picture.