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的GUI框架PySide的安装配置教程
Feb 16 Python
python利用Guetzli批量压缩图片
Mar 23 Python
Python3用tkinter和PIL实现看图工具
Jun 21 Python
python re库的正则表达式入门学习教程
Mar 08 Python
Django集成CAS单点登录的方法示例
Jun 10 Python
Python定时任务随机时间执行的实现方法
Aug 14 Python
PyTorch在Windows环境搭建的方法步骤
May 12 Python
Python Scrapy图片爬取原理及代码实例
Jun 12 Python
python 基于卡方值分箱算法的实现示例
Jul 17 Python
python selenium 获取接口数据的实现
Dec 07 Python
django使用多个数据库的方法实例
Mar 04 Python
pytorch通过训练结果的复现设置随机种子
Jun 01 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
php 日期时间处理函数小结
2009/12/18 PHP
php select,radio和checkbox默认选择的实现方法
2010/05/15 PHP
ubuntu 编译安装php 5.3.3+memcache的方法
2010/08/05 PHP
PHP实现手机号码中间四位用星号(*)隐藏的自定义函数分享
2014/09/27 PHP
php实现xml转换数组的方法示例
2017/02/03 PHP
使用WAMP搭建PHP本地开发环境
2017/05/10 PHP
Jquery 数组操作大全个人总结
2013/11/13 Javascript
Javascript前端UI框架Kit使用指南之kitjs事件管理
2014/11/28 Javascript
jQuery实现表格展开与折叠的方法
2015/05/04 Javascript
jQuery实现指定内容滚动同时左侧或其它地方不滚动的方法
2015/08/08 Javascript
fastclick插件导致日期(input[type="date"])控件无法被触发该如何解决
2015/11/09 Javascript
js添加绑定事件的方法
2016/05/15 Javascript
js实现可输入可选择的select下拉框
2016/12/21 Javascript
vue.js的提示组件
2017/03/02 Javascript
jquery加载单文件vue组件的方法
2017/06/20 jQuery
Vue.js基础指令实例讲解(各种数据绑定、表单渲染大总结)
2017/07/03 Javascript
让你彻底掌握es6 Promise的八段代码
2017/07/26 Javascript
vue二级路由设置方法
2018/02/09 Javascript
使用vue实现各类弹出框组件
2019/07/03 Javascript
python3实现字符串操作的实例代码
2019/04/16 Python
使用python获取(宜宾市地震信息)地震信息
2019/06/20 Python
python实现代码统计器
2019/09/19 Python
wxPython实现整点报时
2019/11/18 Python
Pytorch 使用opnecv读入图像由HWC转为BCHW格式方式
2020/06/02 Python
匡威英国官网:Converse英国
2018/12/02 全球购物
Yves Rocher捷克官方网站:植物化妆品的创造者
2019/07/31 全球购物
建筑实习自我鉴定
2013/10/18 职场文书
学子宴答谢词
2014/01/25 职场文书
会计电算化学生个人的自我评价
2014/02/08 职场文书
端午节演讲稿
2014/05/23 职场文书
旅游与酒店管理专业求职信
2014/07/21 职场文书
2014单位领导班子四风对照检查材料思想汇报
2014/09/25 职场文书
资金申请报告范文
2015/05/14 职场文书
开场白怎么写
2015/06/01 职场文书
千与千寻观后感
2015/06/04 职场文书
教师理论学习心得体会
2016/01/21 职场文书