Python简单实现控制电脑的方法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python简单实现控制电脑的方法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3748字,纯文字阅读大概需要6分钟。
内容图文
![Python简单实现控制电脑的方法](/upload/InfoBanner/zyjiaocheng/430/204c3785e4324286921e0e31582f60bc.jpg)
这篇文章主要介绍了Python简单实现控制电脑的方法,涉及Python基于os及win32api等模块调用系统命令操作电脑的相关实现技巧,需要的朋友可以参考下
本文实例讲述了Python简单实现控制电脑的方法。分享给大家供大家参考,具体如下:
1、windows 下,CMD的一些命令:
dir:列出当前的所有文件
time:打印当前的时间
tree:列出当前目录下的子结构
在cmd中进入了某种模式,退出可以尝试以下命令:q 、exit()、Ctrl+c、Ctrl+z
运行程序:在cmd里面直接输入程序名称。如:notepad、calc
按tab键可以补全名字
在一个文件夹下,想快速打开cmd: 按住shift键,在鼠标点击右键,可以看见命令。
想在cmd中一个文件,但输入名称后显示文件或命令不存在。可以把文件目录加入path环境。
关机: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实现百度语音识别api
用Python实现web端用户登录和注册功能
以上就是Python简单实现控制电脑的方法的详细内容,更多请关注Gxl网其它相关文章!
内容总结
以上是互联网集市为您收集整理的Python简单实现控制电脑的方法全部内容,希望文章能够帮你解决Python简单实现控制电脑的方法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。