在python中使用subprocess调用子进程

本文详细介绍了Python中的subprocess模块,包括不同版本的使用差异,如何使用subprocess.run()执行命令,以及如何捕捉输出。通过实例展示了如何在Ubuntu 18.04上使用Python 3.6执行命令并获取输出。

本博文整理自博友文章https://blog.youkuaiyun.com/qq_33932782/article/details/79929782

例子都在ubuntu 18.04上使用python3.6进行测试。

版本上的说明:python2.4之后有subprocess包,python3.5之后官方建议使用subprocess.run()

1. 不捕捉输出(这种可以用在只执行操作,不需要结果的情况,如建立软连接)

函数:

subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, shell=False, timeout=None, check=False, universal_newlines=False)

  • 如果shell=True,args可以使用字符串,就像写shell一样

>>> subprocess.run("ls -l", shell=True) # doesn't capture output

total 4
drwxr-xr-x  2 chenpan LobeSeg 4096 5月  26 16:14 doc
-rw-r--r--  1 chenpan LobeSeg  483 6月  25 10:27 init.sh
-rw-r--r--  1 chenpan LobeSeg 1070 5月  26 16:14 LICENSE.txt
CompletedProcess(args=['ls', '-l'], returncode=0)

  • 如果shell=False(默认),args可以是字符串的列表或者元组

>>> subprocess.run(["ls", "-l"]) # doesn't capture output

total 4
drwxr-xr-x  2 chenpan LobeSeg 4096 5月  26 16:14 doc
-rw-r--r--  1 chenpan LobeSeg  483 6月  25 10:27 init.sh
-rw-r--r--  1 chenpan LobeSeg 1070 5月  26 16:14 LICENSE.txt
CompletedProcess(args='ls -l', returncode=0)

2. 捕捉输出,以下给出三种实现

  • 函数:

subprocess.getoutput(cmd)

例子:

>>> ret = subprocess.getoutput('ls -l')

>>> print(ret)

total 4
drwxr-xr-x  2 chenpan LobeSeg 4096 5月  26 16:14 doc
-rw-r--r--  1 chenpan LobeSeg  483 6月  25 10:27 init.sh
-rw-r--r--  1 chenpan LobeSeg 1070 5月  26 16:14 LICENSE.txt

  • 函数:

subprocess.getstatusoutput(cmd)

例子:

>>> retcode, output = subprocess.getstatusoutput('ls -l')

>>> print(retcode)

0

>>> print(output)

total 4
drwxr-xr-x  2 chenpan LobeSeg 4096 5月  26 16:14 doc
-rw-r--r--  1 chenpan LobeSeg  483 6月  25 10:27 init.sh
-rw-r--r--  1 chenpan LobeSeg 1070 5月  26 16:14 LICENSE.txt

  • 函数:

class subprocess.Popen(args, bufsize=-1, executable=None, stdin=None, stdout=None, stderr=None,

preexec_fn=None, close_fds=True, shell=False, cwd=None, env=None, universal_newlines=False,

startup_info=None, creationflags=0, restore_signals=True, start_new_session=False, pass_fds=())

例子:

>>> import subprocess
>>> p = subprocess.Popen('ls -l', stdout=subprocess.PIPE, shell=True)
>>> print(p.stdout.read())
b'total 4\ndrwxr-xr-x  2 chenpan LobeSeg 4096 5\xe6\x9c\x88  26 16:14 doc\n-rw-r--r--  1 chenpan LobeSeg  483 6\xe6\x9c\x88  25 10:27 init.sh\n-rw-r--r--  1 chenpan LobeSeg 1070 5\xe6\x9c\x88  26 16:14 LICENSE.txt\n'

Python中可以使用`subprocess`模块调用系统命令来挂载共享盘。`subprocess`模块允许创建新的进程,连接到它们的输入/输出/错误管道,并获取它们的返回码。 以下是一个简单的示例,假设要挂载NFS共享盘: ```python import subprocess try: # 定义挂载命令,这里假设NFS共享盘地址为 192.168.1.100:/shared_folder,挂载点为 /mnt/nfs command = ['mount', '192.168.1.100:/shared_folder', '/mnt/nfs'] # 执行挂载命令 result = subprocess.run(command, check=True, text=True, capture_output=True) print("共享盘挂载成功") print("命令输出:", result.stdout) except subprocess.CalledProcessError as e: print("共享盘挂载失败") print("错误信息:", e.stderr) ``` 在上述代码中,`subprocess.run`函数用于执行挂载命令。`check=True`表示如果命令执行失败(返回非零退出状态码),则抛出`subprocess.CalledProcessError`异常。`text=True`表示以文本模式处理输入输出。`capture_output=True`表示捕获命令的标准输出和标准错误输出。 如果要挂载SMB共享盘,可以使用`mount.cifs`命令,示例如下: ```python import subprocess try: # 定义挂载命令,这里假设SMB共享盘地址为 //192.168.1.100/shared_folder,挂载点为 /mnt/smb command = ['mount.cifs', '//192.168.1.100/shared_folder', '/mnt/smb', '-o', 'username=your_username,password=your_password'] # 执行挂载命令 result = subprocess.run(command, check=True, text=True, capture_output=True) print("共享盘挂载成功") print("命令输出:", result.stdout) except subprocess.CalledProcessError as e: print("共享盘挂载失败") print("错误信息:", e.stderr) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值