Python中subprocess模块的使用
来源:互联网 发布:在淘宝搜血滴子 编辑:程序博客网 时间:2024/05/21 06:25
执行命令:
- >>> subprocess.call(["ls", "-l"])
- 0
- >>> subprocess.call("exit 1", shell=True)
- 1
测试调用系统中cmd命令,显示命令执行的结果:
- x=subprocess.check_output(["echo", "Hello World!"],shell=True)
- print(x)
- "Hello World!"
测试在python中显示文件内容:
- y=subprocess.check_output(["type", "app2.cpp"],shell=True)
- print(y)
- #include <iostream>
- using namespace std;
- ......
- handle = open(r'd:\tmp.log','wt')
- subprocess.Popen(['ipconfig','-all'], stdout=handle)
查看网络设置ipconfig -all,保存到变量中:
- output = subprocess.Popen(['ipconfig','-all'], stdout=subprocess.PIPE,shell=True)
- oc=output.communicate()#取出output中的字符串
- #communicate() returns a tuple (stdoutdata, stderrdata).
- print(oc[0]) #打印网络信息
- Windows IP Configuration
- Host Name . . . . .
- child1 = subprocess.Popen(["dir","/w"], stdout=subprocess.PIPE,shell=True)
- child2 = subprocess.Popen(["wc"], stdin=child1.stdout,stdout=subprocess.PIPE,shell=True)
- out = child2.communicate()
- print(out)
- (' 9 24 298\n', None)
如果想频繁地和子线程通信,那么不能使用communicate();因为communicate通信一次之后即关闭了管道.这时可以试试下面的方法:
- p= subprocess.Popen(["wc"], stdin=subprocess.PIPE,stdout=subprocess.PIPE,shell=True)
- p.stdin.write('your command')
- p.stdin.flush()
- #......do something
- try:
- #......do something
- p.stdout.readline()
- #......do something
- except:
- print('IOError')
- #......do something more
- p.stdin.write('your other command')
- p.stdin.flush()
- #......do something more
0 0
- Python中subprocess模块的使用
- Python中subprocess模块的使用
- Python中subprocess模块的使用
- python的subprocess模块使用
- Python subprocess模块使用
- Python之subprocess模块的使用
- Python的subprocess模块
- python 的 subprocess模块
- 在python中执行外部可执行程序,使用subprocess模块
- python的subprocess模块使用方法
- subprocess模块的使用例子
- python 的 subprocess模块用法 popen
- python subprocess模块的shell参数问题
- python 的 subprocess模块用法 popen
- python 的 subprocess模块用法 popen
- python 的 subprocess模块用法 popen
- python 的 subprocess模块用法 popen
- Python sh模块--------替换subprocess的利器
- java 数据压缩
- UVA 11722 - Joining with Friend(概率)
- 网络基础知识及协议
- Mobile 编译 运行 java 文件步骤
- 文章收藏
- Python中subprocess模块的使用
- 解决ScrollView嵌套ViewPager出现的滑动冲突问题
- Enumeration (C)
- 数据库主外键之NULL值
- 总线初始化及/sys下bus目录的建立
- C数据类型(枚举 enum )和switch语句
- Android中LayoutInflater的使用
- WIN下CMD命令行中输入net start MySQL 出现错误提示
- Android_Intent的使用方法