pythondos命令
‘壹’ python 怎么执行dos命令
写了几个批处理,主要是一些Android调试命令,现在想用python来搞,感觉更酷一些吧。O(∩_∩)O~
比如Ping命令:
ping www..com
用python来做,主要是使用了python标准库中的os库。
参见Python文档The Python Standard Library => Generic Operating System Services => os — Miscellaneous operating system interfaces.
这里介绍的比较全面,而且这个库我们用的也较多。
我们会用到os.system()方法:
[python]view plain
importos
cmd='cmd.exe/kpingwww..com'
os.system(cmd)
‘贰’ 如何在pythonDos模式中进入D盘
按windows和r键打开运行界面,在运行界面输入cmd,按回车进入命令提示符中,在命令提示符中输入D:按回车即可进入D盘。
python怎么设置dos对python中执行DOS命令的方法总结
使用os.system("cmd"),特点是执行的时候程序会打出cmd在linux上执行的信息。使用Popen模块产生新的process,现在大部分人都喜欢使用Popen。Popen方法不会打印出cmd在linux上执行的信息。的确,Popen非常强大,支持多种参数和模式。使用前需要from。
‘叁’ 如何在Python中执行DOS命令
关于python调用cmd命令,主要介绍两种方式:
一、Python的OS模块
OS模块调用CMD命令有两种方式:os.system(),os.popen()。 都是用当前进程来调用。
1、os.system()
a、简单粗暴的执行cmd指令
b、返回结果为0表示执行成功,无法获取命令输出的内容,本在cmd输出的内容会直接在控制台输出。
c、当命令运行结束后接着往下面执行程序(同步阻塞式)。用法如:os.system("ipconfig")。
2、os.popen()
a、能获取DOS命令输出的内容。
b、os.popen()返回的是一个file对象,那么可以跟打开文件一样操作,r是以读的方式打开。
c、当命令运行结束后接着往下面执行程序(同步阻塞式)。
注意:os.popen() 方法用于从一个命令打开一个读写管道,在Unix,Windows中有效。
用法如:
with os.popen(r"adb devices","r")as f:
text = f.read()
print(text)# cmd输出结果
二、管道subprocess模块
a、在当前进程下产生子进程。
b、用wait()函数等待结果(异步非阻塞式)。
【方法1】
result = subprocess.check_output(r'adb devices') ## 命令执行的输出结果放到了result里
print(result)
【方法2】
process = subprocess.Popen(r'adb devices',shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
command_output = process.stdout.read().decode('gbk')
print(command_output)
Notices:
有些命令行是异步执行的不会马上返回输出,所以有时候我们要先等这个命令行执行完毕才能从stdout读出来数据。这个时候要加上
process.wait()
我的GitHub
‘肆’ Python自带IDLE Shell和命令行(cmd中) 清屏方法
一、针对命令行CMD中:调用DOS系统提供的cls命令
import os
os.system('cls')
二、针对IDLE Shell
1、在...\Python\Lib\idlelib文件夹下新建ClearWindow.py文件
class ClearWindow:
menudefs = [
('options', [None,
('Clear Shell Window', '<<clear-window>>'),
]), ]
def __init__(self, editwin):
self.editwin = editwin
self.text = self.editwin.text
self.text.bind("<<clear-window>>", self.clear_window2)
self.text.bind("<<undo>>", self.undo_event) # add="+" doesn't work
def undo_event(self, event):
text = self.text
text.mark_set("iomark2", "iomark")
text.mark_set("insert2", "insert")
self.editwin.undo.undo_event(event)
# fix iomark and insert
text.mark_set("iomark", "iomark2")
text.mark_set("insert", "insert2")
text.mark_unset("iomark2")
text.mark_unset("insert2")
def clear_window2(self, event): # Alternative method
# work around the ModifiedUndoDelegator
text = self.text
text.undo_block_start()
text.mark_set("iomark2", "iomark")
text.mark_set("iomark", 1.0)
text.delete(1.0, "iomark2 linestart")
text.mark_set("iomark", "iomark2")
text.mark_unset("iomark2")
text.undo_block_stop()
if self.text.compare('insert', '<', 'iomark'):
self.text.mark_set('insert', 'end-1c')
self.editwin.set_line_and_column()
def clear_window(self, event):
# remove undo delegator
undo = self.editwin.undo
self.editwin.per.removefilter(undo)
# clear the window, but preserve current command
self.text.delete(1.0, "iomark linestart")
if self.text.compare('insert', '<', 'iomark'):
self.text.mark_set('insert', 'end-1c')
self.editwin.set_line_and_column()
# restore undo delegator
self.editwin.per.insertfilter(undo)
2、 在Python\Lib\idlelib目录下编辑config-extensions.def(IDLE扩展配置文件)
在该文件最后增加如下内容:
[ClearWindow]
enable=1
enable_editor=0
enable_shell=1
[ClearWindow_cfgBindings]
clear-window=<Control-Key-w>
--定义了清屏快捷键Ctrl + w,w必须是小写
启动Python IDLE,在Options菜单下会出现" Clear Shell Window Ctrl+W"
这时候就可以用快捷键Ctrl + w 清屏了
‘伍’ python调用dos命令
换个实现方式吧,这种方式不需要cmd.exe,默认就会执行的:
import os
os.system(r"e: & cd e:\test & dir >> files.txt")
‘陆’ python如何打开上次运行
1 直接命令行启用Python。当然,如果直接在cmd中输入python,需要在windows中的path环境变量中做好设置。此时,cmd中运行python就可以出现 “>>>” 符号。意味着python进入了交互运行模式,可在此模式下做简单的python命令。2. 一种是用它自带的开发环境IDLE。在开始运行处运行命令cmd,进行dos模式,输入python,即可进行python的交互式环境。
3、交互式界面可以用于简单的学习,编写较大程序时应到具体的python文件中,python文件默认的后缀为.py,我们可以新建文本文件,然后把后缀扩展名改为.py,最后选择菜单中的Run下的run mole即可运行,快捷键为F5。
‘柒’ DOS 命令进入 python 目录
把py.bat批处理文件改一下文件名再试一试。不要使用python类似的名字。
很可能是另有一个名为py.exe之类的可执行文件存在。