這篇文章主要介紹了Python如何實現控制電腦,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
具體如下:
1、windows 下,CMD的一些命令:
dir:列出當前的所有文件
time:打印當前的時間
tree:列出當前目錄下的子結構
在cmd中進入了某種模式,退出可以嘗試以下命令:q 、exit()、Ctrl+c、Ctrl+z
運行程序:在cmd里面直接輸入程序名稱。如:notepad、calc
按tab鍵可以補全名字
在一個文件夾下,想快速打開cmd: 按住shift鍵,在鼠標點擊右鍵,可以看見命令。
想在cmd中一個文件,但輸入名稱后顯示文件或命令不存在??梢园盐募夸浖尤雙ath環境。
關機:shutdown -s -t +3600 -c "關機啦!" #3600為時間,即過1小時后關機,并且在屏幕上顯示“關機啦!”
取消關機命令:shutdown -a
2、Python控制cmd
2.1、os.system('xxx')
xxx為在cmd中執行的命令
2.2、 subprocess.Popen('xxx',shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
xxx為在cmd中執行的命令,其他不用改。
例子:
# -*- 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
是一步一步打印出來,而 subprocess.Popen
則一次性返回最終結果。
在目錄下下建一個文件 conf.txt。在文件里面輸入 ping www.baidu.com
# -*- 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)
會看見程序每5秒運行 ping一次。改動conf.txt里面的內容為dir ,發現程序不再ping,而是打印文件夾的文件名稱。
3、Python模塊 win32api
3.1、win32api.Beep
Beep(freq, dur) freq代表頻率,dur代表持續的時間。
# -*- coding: utf-8 -*- import win32api win32api.Beep(6000,3000)
會持續三秒聽見吱吱的響聲
3.2、win32api.MessageBox
MessageBox(hwnd, message , title , style , language )
會彈出一個窗口
hwnd : int 從哪個位置彈出窗口。一般為0
message : 窗口內容
title : 標題名字
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 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中的內容: ”1 # hi ,beautiful girl# how are you!”
彈出一個顯示名稱為“how are you!” ,內容為“ hi ,beautiful girl”的窗口。
3.3、win32api.ShellExecute
int = ShellExecute(hwnd, op , file , params , dir , bShow ) 執行程序
hwnd : intint 從哪個位置彈出窗口。一般為0
op : string 操作符。The operation to perform. May be "open", "print", or None, which defaults to "open".
file : string 文件的地址。The name of the file to open.
params : string??梢詾榭?。The parameters to pass, if the file name contains an executable. Should be None for a document file.
dir : string??梢詾榭?。The initial directory for the application.
bShow : int 。1 表示打開窗口;0 表示不打開。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 win32api win32api.ShellExecute(0,'open',r'C:\Users\Administrator\Pictures\toutiao\1.jpg','','',1)
運行程序就會打開這張圖片。
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Python如何實現控制電腦”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。