Python 调用 Shell脚本的方法
Python 调用 Shell脚本的方法
1.os模块的popen方法
通过 os.popen() 返回的是 file read 的对象,对其进行读取 read() 的操作可以看到执行的输出。
>>> os.popen('date -u |wc')
<open file 'date -u |wc', mode 'r' at 0x7f9539eb34b0>
>>> os.popen('date -u |wc').read()
' 1 6 43\n'
2.利用commands模块
这个模块有个非常好用的方法可以直接读取程序执行的返回值
通过 commands.getstatusoutput() 一个方法就可以获得到返回值和输出
>>> import commands
>>> commands.getstatusoutput('ls /bin/ls')
(0, '/bin/ls')
>>> commands.getstatusoutput('cat /bin/junk')
(256, 'cat: /bin/junk: No such file or directory')
>>> commands.getstatusoutput('/bin/junk')
(256, 'sh: /bin/junk: not found')
>>> commands.getoutput('ls /bin/ls')
'/bin/ls'
>>> commands.getstatus('/bin/ls')
'-rwxr-xr-x 1 root 13352 Oct 14 1994 /bin/ls'
3.利用subprocess模块
subprocess模块用来启动可终止其它程序,创建多个进程.想要shell中运行其它程序并获取它的输出,可以使用check_output()方法,它接受一个命令和参数列表
>>> subprocess.check_output(["echo", "Hello World!"])
'Hello World!\n'
>>> ret = subprocess.check_output(['date','-u'])
>>> ret
'2016\xe5\xb9\xb4 05\xe6\x9c\x88 20\xe6\x97\xa5 \xe6\x98\x9f\xe6\x9c\x9f\xe4\xba\x94 09:48:44 UTC\n'
本文转载自:https://blog.csdn.net/u010786109/article/details/51463598
Python 调用 Shell脚本的方法的更多相关文章
- 3种python调用其他脚本的方法,你还知道其他的方法吗?
1.用python调用python脚本 #!/usr/local/bin/python3.7 import time import os count = 0 str = ('python b.py') ...
- python调用shell脚本时需要切换目录
最近遇到了一个问题,就是python代码调用shell脚本时,发现输入输出的文件,总是和自己预想的有偏差,但是单独在linux下执行命令的时候,却没有错误.后来发现是相对路径的问题,因为执行pytho ...
- python调用shell脚本
# coding=utf-8 //设置文本格式import os //导入os方法print('hello')n=os.system('/home/csliyb/kjqy_x ...
- python 调用shell命令的方法
在python程序中调用shell命令,是件很酷且常用的事情…… 1. os.system(command) 此函数会启动子进程,在子进程中执行command,并返回command命令执行完毕后的退出 ...
- python 调用shell命令三种方法
#!/usr/bin/python是告诉操作系统执行这个脚本的时候,调用/usr/bin下的python解释器: #!/usr/bin/env python这种用法是为了防止操作系统用户没有将pyth ...
- python 调用 shell 命令
记录 python 调用 shell 命令的方法 加载 os 模块, 使用 os 类 import os; os.system("ls /");
- 【原】Gradle调用shell脚本和python脚本并传参
最近由于项目自动化构建的需要,研究了下gradle调用脚本并传参的用法,在此作个总结. Pre build.gradle中定义了$jenkinsJobName $jenkinsBuild两个Jenki ...
- python 调用 shell 命令方法
python调用shell命令方法 1.os.system(cmd) 缺点:不能获取返回值 2.os.popen(cmd) 要得到命令的输出内容,只需再调用下read()或readlines()等 ...
- [Python]在python中调用shell脚本,并传入参数-02python操作shell实例
首先创建2个shell脚本文件,测试用. test_shell_no_para.sh 运行时,不需要传递参数 test_shell_2_para.sh 运行时,需要传递2个参数 test_shell ...
随机推荐
- time函数
time函数 time #include<time.h> time_t time(time_t *t); typdef long int time_t; time() returns th ...
- debug宏起作用应用
在linux内核中重新定义了printk,如pr_debug,dev_dbg等.要使用这些宏函数就需要定义DEBUG. 详见:kernel printk信息显示级别 那么DEBUG该定义在什么地方呢? ...
- (转)maven3.3.9编译oozie4.3.0
1.Java版本1.8 [root@sht-sgmhadoopdn-04 app]# java -versionjava version "1.8.0_66"Java(TM) SE ...
- 特征根法求通项+广义Fibonacci数列找循环节 - HDU 5451 Best Solver
Best Solver Problem's Link Mean: 给出x和M,求:(5+2√6)^(1+2x)的值.x<2^32,M<=46337. analyse: 这题需要用到高中的数 ...
- mongodb和redis设计原理简析
转自:http://blog.csdn.net/yangbutao/article/details/8309539 redis: 1.NIO通信 因都在内存操作,所以逻辑的操作非常快,减少 ...
- js函数柯里化
function curry(fn){ // 代码 } function add(a,b,c){ return a + b + c; } const execAdd = curry(add); exe ...
- 打印出现:You've implemented -[<UIApplicationDelegate> application:didReceiveRemoteNotification:
解决办法: 第一种:Product -> Target -> Capabilities -> Background Modes -> 勾选Remote notification ...
- uva 110 Meta-Loopless Sorts 用程序写程序 有点复杂的回溯水题
题目要求写一个直接用比较排序的pascal程序,挺有趣的一题. 我看题目数据范围就到8,本来以为贪个小便宜,用switch输出. 然后发现比较次数是阶乘级别的,8的阶乘也是挺大的,恐怕会交不上去. 于 ...
- 【BZOJ4598】[Sdoi2016]模式字符串 树分治+hash
[BZOJ4598][Sdoi2016]模式字符串 Description 给出n个结点的树结构T,其中每一个结点上有一个字符,这里我们所说的字符只考虑大写字母A到Z,再给出长度为m的模式串s,其中每 ...
- ios iphone、ipad启动画面尺寸
以下是iphone.ipad启动画面的尺寸 iphone4(纵):320 x 480 iphone4 Retina(纵):640 x 960 iphone5(纵):320 x 568 iphone ...