python指定目录运行shell_python(6)-执行shell命令

本文介绍了Python如何使用subprocess模块执行shell命令,包括call、check_call、check_output和Popen方法的使用,并给出了在指定目录下执行命令及处理输入输出的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

可以执行shell命令的相关模块和函数有:os.system

os.spawn*

os.popen*          --废弃

popen2.*           --废弃

commands.*      --废弃,3.x中被移除

上面这些命令,可以使用subprocess完美的实现,而且具有丰富的功能:

call:   python3.5以下才有, python3.5及以上变成run方法

执行命令,返回状态码

>>> a = subprocess.call('whoami')

huangxm-pc\huangxm>>> print(a)

0

执行一个带参数的命令

>>> subprocess.call('ls -l')

Traceback (most recent call last):

File"", line 1, in

报错了,对于这种可以加上shell=True, 表示完全当成shell命令执行

>>> subprocess.call('ls -l', shell=True)

total48drwxr-xr-x 5 huanghao huanghao 4096 Mar 12 18:42day7

drwxrwxrwx2 huanghao huanghao 4096 Oct 19 22:42 Desktop

check_call

执行命令,如果执行状态码是 0 ,则返回0,否则抛异

>>> subprocess.check_call('cat /etc/passwd | grep root', shell=True)

root:x:0:0:root:/root:/bin/bash

0>>> subprocess.check_call('cat /etc/passwddd | grep root', shell=True) #执行失败,抛出异常

cat:/etc/passwddd: No such file ordirectory

Traceback (most recent call last):

File"", line 1, in File"/usr/lib/python3.4/subprocess.py", line 557, incheck_callraiseCalledProcessError(retcode, cmd)

subprocess.CalledProcessError: Command'cat /etc/passwddd | grep root' returned non-zero exit status 1

check_output

执行命令,如果执行状态码是 0 ,则返回0,否则抛异

subprocess.Popen(...)

用于执行复杂的系统命令

参数:args:shell命令,可以是字符串或者序列类型(如:list,元组)

bufsize:指定缓冲。0 无缓冲,1 行缓冲,其他 缓冲区大小,负值 系统缓冲

stdin, stdout, stderr:分别表示程序的标准输入、输出、错误句柄

preexec_fn:只在Unix平台下有效,用于指定一个可执行对象(callable object),它将在子进程运行之前被调用

close_sfs:在windows平台下,如果close_fds被设置为True,则新创建的子进程将不会继承父进程的输入、输出、错误管道。

所以不能将close_fds设置为True同时重定向子进程的标准输入、输出与错误(stdin, stdout, stderr)。

shell:同上

cwd:用于设置子进程的当前目录

env:用于指定子进程的环境变量。如果env = None,子进程的环境变量将从父进程中继承。

universal_newlines:不同系统的换行符不同,True -> 同意使用 \n

startupinfo与createionflags只在windows下有效

将被传递给底层的CreateProcess()函数,用于设置子进程的一些属性,如:主窗口的外观,进程的优先级等等

a = subprocess.Popen('ipconfig', shell=True)print(a)

执行结果

命令成功执行了,但是没有获得到结果,怎么获得结果呢?

a = subprocess.Popen('ipconfig', shell=True, stdout=subprocess.PIPE).stdout.read()print(str(a, 'gbk')) #winodws中文是gbk

有时候我们并不是只这样运行命令,而是需要进入一些目录去执行一些命令,比如我们要在D盘建立一个文件夹:

a = subprocess.Popen('mkdir aaaaa', shell=True, cwd='C:/')

执行之后,到C盘看一下,有没有一个aaaaa的文件夹。

再来看一个复杂点的:

第一步:先在D盘根目录建立一个文件aa.py,内容如下:

defadd():

x= input('input x:').strip()print('\n')

y= input('input y:').strip()print('\n')print('the result is', int(x)+int(y))if __name__ == "__main__":

add()

第二步:执行下面的代码

obj = subprocess.Popen('python3 D:/aa.py', shell=True, cwd='c:/python34', stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

obj.stdin.write(b'1\n')

obj.stdin.write(b'2\n')

obj.stdin.close()

cmd_out= str(obj.stdout.read(), 'gbk')

obj.stdout.close()print(cmd_out)

第三步:查看执行结果

input x:

input y:

the resultis 3

在提示要输入的时候,我们根本就没有在键盘上输入,因为stdin.write已经帮我们完成了输入。 然后程序通过stdout.read()将结果读取出来。如果程序执行中发生错误,还可以能过obj.stderr.read()获取错误信息。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值