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 冒泡,选择,插入排序使用实例
Feb 05 Python
Python3搜索及替换文件中文本的方法
May 22 Python
Python入门_浅谈逻辑判断与运算符
May 16 Python
在python中使用正则表达式查找可嵌套字符串组
Oct 24 Python
python中使用PIL制作并验证图片验证码
Mar 15 Python
Python爬虫信息输入及页面的切换方法
May 11 Python
Matplotlib 生成不同大小的subplots实例
May 25 Python
Python实现的连接mssql数据库操作示例
Aug 17 Python
Python Web版语音合成实例详解
Jul 16 Python
python模式 工厂模式原理及实例详解
Feb 11 Python
python入门之井字棋小游戏
Mar 05 Python
Python中logger日志模块详解
Aug 04 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
与数据库连接
2006/10/09 PHP
linux下安装php的memcached客户端
2014/08/03 PHP
php实现utf-8转unicode函数分享
2015/01/06 PHP
Laravel 5.5 的自定义验证对象/类示例代码详解
2017/08/29 PHP
phpstudy的php版本自由修改的方法
2017/10/18 PHP
深入学习微信网址链接解封的防封原理visit_type
2019/08/15 PHP
Alliance vs AM BO3 第一场2.13
2021/03/10 DOTA
在JavaScript中,为什么要尽可能使用局部变量?
2009/04/06 Javascript
起点页面传值js,有空研究学习下
2010/01/25 Javascript
jquery异步循环获取功能实现代码
2010/09/19 Javascript
IE中的File域无法清空使用jQuery重设File域
2014/04/24 Javascript
详解JavaScript的表达式与运算符
2015/11/30 Javascript
信息页文内画中画广告js实现代码(文中加载广告方式)
2016/01/03 Javascript
Angular2  NgModule 模块详解
2016/10/19 Javascript
jQuery中table数据的值拷贝和拆分
2017/03/19 Javascript
Vue.directive()的用法和实例详解
2018/03/04 Javascript
vue .sync修饰符的使用详解
2018/06/15 Javascript
jQuery中常用动画效果函数知识点整理
2018/08/19 jQuery
解决angular2 获取到的数据无法实时更新的问题
2018/08/31 Javascript
解决vue 打包发布去#和页面空白的问题
2018/09/04 Javascript
vue-cli项目无法用本机IP访问的解决方法
2018/09/20 Javascript
JavaScript如何使用插值实现图像渐变
2020/06/28 Javascript
Python运行的17个时新手常见错误小结
2012/08/07 Python
通过数据库向Django模型添加字段的示例
2015/07/21 Python
windows下python连接oracle数据库
2017/06/07 Python
Python程序员面试题 你必须提前准备!
2018/01/16 Python
Python 使用元类type创建类对象常见应用详解
2019/10/17 Python
详解Java中一维、二维数组在内存中的结构
2021/02/11 Python
html5 迷宫游戏(碰撞检测)实例一
2013/07/25 HTML / CSS
html5的canvas方法使用指南
2014/12/15 HTML / CSS
Roxy美国官网:澳大利亚冲浪、滑雪健身品牌
2016/07/30 全球购物
耐克美国官网:Nike.com
2016/08/01 全球购物
总经理的岗位职责
2014/02/23 职场文书
个人廉洁自律承诺书
2014/03/27 职场文书
高中生家长寄语大全
2014/04/03 职场文书
团支部推优材料
2014/05/21 职场文书