Python簡單實現(xiàn)控制電腦的方法
本文實例講述了Python簡單實現(xiàn)控制電腦的方法。分享給大家供大家參考,具體如下:
1、windows 下,CMD的一些命令:
dir:列出當前的所有文件
time:打印當前的時間
tree:列出當前目錄下的子結(jié)構(gòu)
在cmd中進入了某種模式,退出可以嘗試以下命令:q 、exit()、Ctrl+c、Ctrl+z
運行程序:在cmd里面直接輸入程序名稱。如:notepad、calc
按tab鍵可以補全名字
在一個文件夾下,想快速打開cmd: 按住shift鍵,在鼠標點擊右鍵,可以看見命令。
想在cmd中一個文件,但輸入名稱后顯示文件或命令不存在??梢园盐募夸浖尤雙ath環(huán)境。
關(guān)機:shutdown -s -t +3600 -c "關(guān)機啦!" #3600為時間,即過1小時后關(guān)機,并且在屏幕上顯示“關(guān)機啦!”
取消關(guān)機命令:shutdown -a
2、Python控制cmd
2.1、os.system('xxx') xxx為在cmd中執(zhí)行的命令
2.2、 subprocess.Popen('xxx',shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
xxx為在cmd中執(zhí)行的命令,其他不用改。
例子:
# -*- 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則一次性返回最終結(jié)果。
在目錄下下建一個文件 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里面的內(nèi)容為dir ,發(fā)現(xiàn)程序不再ping,而是打印文件夾的文件名稱。
3、Python模塊 win32api
3.1、win32api.Beep
Beep(freq, dur) freq代表頻率,dur代表持續(xù)的時間。
# -*- coding: utf-8 -*- import win32api win32api.Beep(6000,3000)
會持續(xù)三秒聽見吱吱的響聲
3.2、win32api.MessageBox
MessageBox(hwnd, message , title , style , language ) 會彈出一個窗口
hwnd : int 從哪個位置彈出窗口。一般為0
message : 窗口內(nèi)容
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中的內(nèi)容: ”1 # hi ,beautiful girl# how are you!”
彈出一個顯示名稱為“how are you!” ,內(nèi)容為“ hi ,beautiful girl”的窗口。
3.3、win32api.ShellExecute
int = ShellExecute(hwnd, op , file , params , dir , bShow ) 執(zhí)行程序
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)
運行程序就會打開這張圖片。
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python進程與線程操作技巧總結(jié)》、《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
Python中raise用法簡單實例(超級詳細,看了無師自通)
python中raise語句用于手動觸發(fā)異常,通過raise語句可以在代碼中顯式地引發(fā)異常,從而使程序進入異常處理流程,下面這篇文章主要給大家介紹了關(guān)于Python中raise用法的相關(guān)資料,需要的朋友可以參考下2024-03-03
Python?提取出SQL語句中Where的值兩種方法(示例代碼)
為了提取SQL語句中WHERE子句的值,我們可以利用Python的sqlparse庫,這是一個專門用于解析SQL語句的庫,這篇文章主要介紹了Python?提取出SQL語句中Where的值的方法,需要的朋友可以參考下2024-08-08
Python操作SQLite數(shù)據(jù)庫的方法詳解【導(dǎo)入,創(chuàng)建,游標,增刪改查等】
這篇文章主要介紹了Python操作SQLite數(shù)據(jù)庫的方法,簡單說明了sqlite數(shù)據(jù)庫的相關(guān)概念,并結(jié)合實例形式較為詳細的分析了Python針對sqlite數(shù)據(jù)庫的導(dǎo)入,創(chuàng)建,游標,增刪改查等操作技巧,需要的朋友可以參考下2017-07-07
如何使用Python生成4位數(shù)的隨機數(shù)字
本文討論了如何使用randint() 和randrange() 方法來生成一個四位數(shù)的數(shù)字,此外,我們還討論了另一種擁有隨機四位數(shù)號碼的途徑,感興趣的朋友跟隨小編一起看看吧2023-10-10

