Python简单实现控制电脑的方法


Posted in Python onJanuary 22, 2018

本文实例讲述了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程序设计有所帮助。

Python 相关文章推荐
在Python中操作字典之clear()方法的使用
May 21 Python
Python之父谈Python的未来形式
Jul 01 Python
Ubuntu 下 vim 搭建python 环境 配置
Jun 12 Python
Python 处理数据的实例详解
Aug 10 Python
Python requests发送post请求的一些疑点
May 20 Python
Django框架设置cookies与获取cookies操作详解
May 27 Python
python类中super() 的使用解析
Dec 19 Python
关于pytorch中全连接神经网络搭建两种模式详解
Jan 14 Python
python实现FTP文件传输的方法(服务器端和客户端)
Mar 20 Python
Django高并发负载均衡实现原理详解
Apr 04 Python
构建高效的python requests长连接池详解
May 02 Python
Python文件夹批处理操作代码实例
Jul 21 Python
Zookeeper接口kazoo实例解析
Jan 22 #Python
Python调用C语言的方法【基于ctypes模块】
Jan 22 #Python
python的Crypto模块实现AES加密实例代码
Jan 22 #Python
python实现求最长回文子串长度
Jan 22 #Python
Python获取本机所有网卡ip,掩码和广播地址实例代码
Jan 22 #Python
Linux CentOS7下安装python3 的方法
Jan 21 #Python
简述Python2与Python3的不同点
Jan 21 #Python
You might like
无线电波是什么?它是怎样传输的?
2021/03/01 无线电
PHP修改session_id示例代码
2014/01/08 PHP
用PHP解决的一个栈的面试题
2014/07/02 PHP
利用Ext Js生成动态树实例代码
2008/09/08 Javascript
JQuery 国际象棋棋盘 实现代码
2009/06/26 Javascript
div层的移动及性能优化
2010/11/16 Javascript
Bootstrap的图片轮播示例代码
2015/08/31 Javascript
javascript伸缩菜单栏实现代码分享
2015/11/12 Javascript
使用jQuery+EasyUI实现CheckBoxTree的级联选中特效
2015/12/06 Javascript
jQuery实现带水平滑杆的焦点图动画插件
2016/03/08 Javascript
canvas实现手机端用来上传用户头像的代码
2016/10/20 Javascript
html判断当前页面是否在iframe中的实例
2016/11/30 Javascript
layer页面跳转,获取html子节点元素的值方法
2019/09/27 Javascript
jquery添加div实现消息聊天框
2020/02/08 jQuery
[04:00]DOTA2解说界神雕侠侣 CJ第四天谷子现场过生日
2013/07/30 DOTA
python利用elaphe制作二维条形码实现代码
2012/05/25 Python
用virtualenv建立多个Python独立虚拟开发环境
2017/07/06 Python
Python实现的当前时间多加一天、一小时、一分钟操作示例
2018/05/21 Python
Selenium+Python 自动化操控登录界面实例(有简单验证码图片校验)
2019/06/28 Python
利用python 下载bilibili视频
2020/11/13 Python
python 实现波浪滤镜特效
2020/12/02 Python
CSS3之背景尺寸Background-size使用介绍
2013/10/14 HTML / CSS
英国床垫在线:Mattress Online
2016/12/07 全球购物
美国男士内衣品牌:Tommy John
2017/12/22 全球购物
Strawberrynet草莓网新加坡站:护肤、彩妆、香水及美发产品
2018/08/31 全球购物
澳大利亚优质葡萄酒专家:Vintage Cellars
2019/01/08 全球购物
西班牙三叶草药房:Farmacias Trébol
2019/05/03 全球购物
包装类的功能、种类、常用方法
2012/01/27 面试题
金融专业推荐信
2013/11/14 职场文书
技校个人求职信范文
2014/01/25 职场文书
气象学专业个人求职信
2014/04/22 职场文书
党的群众路线教育实践活动宣传标语口号
2014/06/06 职场文书
国际贸易毕业生求职信
2014/07/20 职场文书
《微笑着面对生活》优秀演讲稿范文
2014/09/23 职场文书
高中生第一学年自我鉴定2015
2014/09/28 职场文书
《活见鬼》教学反思
2016/02/24 职场文书