Python simple method to control the computer

  • 2020-07-21 08:57:10
  • OfStack

An example of Python is presented in this paper. To share for your reference, specific as follows:

1, windows, CMD 1, windows

dir: Lists all the current files

time: Prints the current time

tree: Lists the substructures under the current directory

In cmd, you enter a mode where you can try the following commands to exit: q, exit(), Ctrl+c, Ctrl+z

Run the program: Enter the program name directly in cmd. For example, notepad, calc

Press tab to complete the name

In 1 folder, to quickly open cmd: Hold down the shift key, and click the right mouse button to see the commands.

Want 1 file in cmd, but enter the name and the file or command does not exist. You can add file directories to the path environment.

Power off: shutdown-ES43en-ES44en + 3600-ES45en "Power off!" Is the time, that is, after 1 hour shut down, and on the screen display "shut down!"

Cancel shutdown command: ES48en-ES49en

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 executed in cmd, nothing else is changed.

Example:


# -*- coding: utf-8 -*-
import os
os.system("ping www.baidu.com")


# -*- coding: utf-8 -*-
import subprocess
a=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 It's step 1 step 1 print it out, and subprocess.Popen The first sex returns the final result.

Create a file under the directory conf.txt. Enter ping www.baidu.com in the file


# -*- coding: utf-8 -*-
import os
import 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 i
while True:
  f = open('conf.txt', 'r')
  content = f.read()
  os.system(content)
  time.sleep(5)

You should see the program run ping1 every 5 seconds. Change conf.txt to dir, find the program is no longer ping, but print the file name of the folder.

3. Python module win32api

3.1, win32api Beep

Beep(freq, dur) freq stands for frequency and dur for duration.


# -*- coding: utf-8 -*-
import win32api
win32api.Beep(6000,3000)

It will squeak for 3 seconds

3.2, win32api MessageBox

MessageBox(hwnd, message , title , style , language ) A window pops up

hwnd: From which location does int pop up? 1 to 0

message: Window content

title: Title name

style=win32con.MB_OK: int, The style the message box.

language=win32api.MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT) : int, The ID to use.


# -*- coding: utf-8 -*-
import win32api
import 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 Contents of:   " 1 # hi ,beautiful girl# how are you! " 

Pop up a display named "how are you!" , hi,beautiful girl.

3.3, win32api ShellExecute

int = ShellExecute(hwnd, op, file, params, dir, bShow) to execute the procedure

hwnd: Where does intint pop up from? 1 to 0

op: string operator. The operation to perform "open", "print", or None, which defaults to "open".

file: The address of the string file. The name the file to open.

params: string. It could be empty. The to pass, if the name executable an executable

dir: string. It could be empty. The initial directory for the application

bShow: int. 1 means open window; Zero means not open. Specifies the is is is is is parameter parameter parameter parameter parameter parameter parameter


# -*- coding: utf-8 -*-
import win32api
win32api.ShellExecute(0,'open',r'C:\Users\Administrator\Pictures\toutiao\1.jpg','','',1)

Running the program will open up this picture.

More about Python related topics: interested readers to view this site "Python process and thread skills summary", "Python Socket programming skills summary", "Python data structure and algorithm tutorial" and "Python function using techniques", "Python string skills summary", "Python introduction and advanced tutorial" and "Python file and directory skills summary"

I hope this article has been helpful for Python programming.


Related articles: