Python is a simple method to control computers, and python is used to control computers.
This article describes how to control computers in Python. We will share this with you for your reference. The details are as follows:
1. Some CMD commands in windows:
Dir: list all current files
Time: print the current time
Tree: list sub-structures under the current directory
Enter a certain mode in cmd. to exit, try the following commands: q, exit (), Ctrl + c, Ctrl + z
Run the program: Enter the program name in cmd. Such as notepad and calc
Press the tab key to complete the name
In a folder, you can quickly open cmd: press the shift key and right-click to view the command.
If you want to create a file in cmd, but enter a name, the file or command does not exist. You can add the file directory to the path environment.
Shutdown: shutdown-s-t + 3600-c "shutdown! "#3600 is the time, that is, shutdown after 1 hour, and" shutdown! "is displayed on the screen !"
Command to cancel shutdown: shutdown-
2. Python command 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 executed in cmd. You do not need to modify other commands.
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.systemIs printed step by step, andsubprocess.PopenThe final result is returned at a time.
Create a file conf.txt under the directory. 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 = subprocess.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 can see that the program runs ping once every five seconds. Change the content in conf.txt to dir, and find that the program does not ping, But prints the file name of the folder.
3. win32api of Python Module
3.1. win32api. Beep
Beep (freq, dur) freq represents the frequency, and dur represents the duration.
# -*- coding: utf-8 -*-import win32apiwin32api.Beep(6000,3000)
It will take three seconds to hear the noise
3.2. win32api. MessageBox
MessageBox(hwnd, message , title , style , language )A window will pop up.
Hwnd: position where the int is located. Generally 0
Message: Window content
Title: title 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 are you !"
A display name "how are you!" is displayed !" , The content is the "hi, beauul ul girl" window.
3.3. win32api. ShellExecute
Int = ShellExecute (hwnd, op, file, params, dir, bShow) Execution Program
Hwnd: position from which intint is displayed. Generally 0
Op: string operator. The operation to perform. May be "open", "print", or None, which defaults to "open ".
File: string file address. The name of the file to open.
Params: string. It can be null. The parameters to pass, if the file name contains an executable. shocould be None for a document file.
Dir: string. It can be null. The initial directory for the application.
BShow: int. 1 indicates that the window is opened; 0 indicates that the window is not opened. Specifies whether the application is shown when it is 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)
The running program will open this picture.