一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Python簡單實現控制電腦的方法

Python簡單實現控制電腦的方法

2021-01-07 00:18Lovephysics Python

這篇文章主要介紹了Python簡單實現控制電腦的方法,涉及Python基于os及win32api等模塊調用系統(tǒng)命令操作電腦的相關實現技巧,需要的朋友可以參考下

本文實例講述了Python簡單實現控制電腦的方法。分享給大家供大家參考,具體如下:

1、windows 下,CMD的一些命令:

dir:列出當前的所有文件

time:打印當前的時間

tree:列出當前目錄下的子結構

在cmd中進入了某種模式,退出可以嘗試以下命令:q 、exit()、Ctrl+c、Ctrl+z

運行程序:在cmd里面直接輸入程序名稱。如:notepad、calc

按tab鍵可以補全名字

在一個文件夾下,想快速打開cmd: 按住shift鍵,在鼠標點擊右鍵,可以看見命令。

想在cmd中一個文件,但輸入名稱后顯示文件或命令不存在。可以把文件目錄加入path環(huán)境。

關機:shutdown -s -t +3600 -c "關機啦!"            #3600為時間,即過1小時后關機,并且在屏幕上顯示“關機啦!”

取消關機命令: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í)行的命令,其他不用改。

例子:

?
1
2
3
# -*- coding: utf-8 -*-
import os
os.system("ping www.baidu.com")
?
1
2
3
4
5
6
# -*- 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# -*- 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 ,發(fā)現程序不再ping,而是打印文件夾的文件名稱。

3、Python模塊 win32api

3.1、win32api.Beep

Beep(freq, dur)     freq代表頻率,dur代表持續(xù)的時間。

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

會持續(xù)三秒聽見吱吱的響聲

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.

?
1
2
3
4
5
6
7
8
9
10
11
# -*- 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 )   執(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.

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

運行程序就會打開這張圖片。

希望本文所述對大家Python程序設計有所幫助。

原文鏈接:http://www.cnblogs.com/lovephysics/p/7239934.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲国产成人精品无码区99 | 91影视永久福利免费观看 | 成人国产一区 | 欧美亚洲国产精品久久久 | 色悠久久久久综合网小说 | 沉沦艳妇杨幂肉体小说 | 黄色a| 白丝打脚枪 | 久久热在线视频精品店 | 女教师系列三上悠亚在线观看 | 国产 日韩欧美 | 精品无码一区二区三区中文字幕 | 古代翁熄系小说辣文 | 国内精品久久久久久久久久久久 | 成人精品在线 | 欧美精品一区二区三区免费观看 | japanesen女同 | 欧美日韩国产在线人成 | 刺客女仆 | 国产精品igao视频网网址 | 亚洲精品一区制服丝袜 | 日本一区二区视频在线 | 免费国产午夜高清在线视频 | 国产精品成人在线播放 | 黑人巨大精品战中国美女 | 亚洲 日本 中文字幕 制服 | 欧美精品v日韩精品v国产精品 | 91无套极品外围在线播放 | 王小军怎么了最新消息 | 无人区尖叫之夜美女姐姐视频 | beeg日本高清xxxx18 | 99在线观看视频免费精品9 | 久久精品动漫网一区二区 | 动漫美女人物被黄漫在线看 | 国产欧美一区二区精品性色 | 视频一区国产精戏刘婷30 | 男女姓交大视频免费观看 | 免费看黄色一级 | 国产亚洲精品一区二区在线播放 | ipx 在线播放 | yjsp妖精视频在线观看免费 |