GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.runoob.com — Python subprocess 模块 | 菜鸟教程
Python 3 教程
Python3 教程
Python3 简介
Python3 环境搭建
Python3 Pycharm
Python3 VScode
Python3 基础语法
Python3 基本数据类型
Python3 数据类型转换
Python3 解释器
Python3 注释
Python3 运算符
Python3 数字(Number)
Python3 字符串
Python3 列表
Python3 元组
Python3 字典
Python3 集合
Python3 条件控制
Python3 循环语句
Python3 编程第一步
Python3 推导式
Python3 迭代器与生成器
Python3 with
Python3 函数
Python3 lambda
Python3 装饰器
Python3 数据结构
Python3 模块
Python __name__
Python3 输入和输出
Python3 File
Python3 OS
Python3 错误和异常
Python3 面向对象
Python3 命名空间/作用域
Python 虚拟环境的创建
Python 类型注解
Python3 标准库概览
Python3 实例
Python 测验
Python3 高级教程Python3 正则表达式 Python3 CGI编程 Python3 MySQL(mysql-connector) Python3 MySQL(PyMySQL) Python3 网络编程 Python3 SMTP发送邮件 Python3 多线程 Python3 XML 解析 Python3 JSON Python3 日期和时间 Python3 内置函数 Python3 MongoDB Python3 urllib Python uWSGI 安装配置 Python3 pip Python3 operator Python math Python requests Python random Python OpenAI Python 有用的资源 Python AI 绘画 Python statistics Python hashlib Python 量化 Python pyecharts Python selenium 库 Python 爬虫 Python Scrapy 库 Python Markdown Python sys 模块 Python Pickle 模块 Python subprocess 模块 Python queue 模块 Python StringIO 模块 Python logging 模块 Python datetime 模块 Python re 模块 Python csv 模块 Python threading 模块 Python asyncio 模块 Python PyQtPython subprocess 模块
通过 为什么使用 subprocess 模块?在 Python 中,有时我们需要执行一些系统命令或调用其他程序来完成特定的任务。例如,你可能需要运行一个 shell 命令、启动一个外部应用程序,或者与一个命令行工具进行交互。 与早期的 subprocess 模块的核心功能1. 执行外部命令
实例
import subprocess
# 执行一个简单的 shell 命令 result = subprocess.run(['ls', '-l'], capture_output=True, text=True) # 打印命令的输出 print(result.stdout) 在这个例子中, 2. 处理输入和输出
实例
import subprocess
# 执行一个命令,并将输入传递给子进程 result = subprocess.run(['grep', 'python'], input='hello\npython\nworld', capture_output=True, text=True) # 打印命令的输出 print(result.stdout) 在这个例子中, 3. 处理错误
实例
import subprocess
try: result = subprocess.run(['ls', 'nonexistent_file'], capture_output=True, text=True, check=True) except subprocess.CalledProcessError as e: print(f"Command failed with return code {e.returncode}") print(f"Error output: {e.stderr}") 在这个例子中, subprocess 模块的高级用法1. 使用 Popen 类
实例
import subprocess
# 启动一个子进程 process = subprocess.Popen(['ping', 'google.com'], stdout=subprocess.PIPE, text=True) # 读取子进程的输出 while True: output = process.stdout.readline() if output == '' and process.poll() is not None: break if output: print(output.strip()) # 获取子进程的退出状态码 return_code = process.poll() print(f"Process finished with return code {return_code}") 在这个例子中, 2. 使用管道
实例
import subprocess
# 使用管道连接两个命令 p1 = subprocess.Popen(['ls', '-l'], stdout=subprocess.PIPE) p2 = subprocess.Popen(['grep', 'py'], stdin=p1.stdout, stdout=subprocess.PIPE, text=True) # 获取最终输出 output = p2.communicate()[0] print(output) 在这个例子中, subprocess 模块的常用方法、类和参数以下是 Python subprocess 模块的常用方法、类和参数的说明,包含功能描述及示例: subprocess 模块核心方法
subprocess.CompletedProcess 对象属性(run() 方法的返回对象)
subprocess.Popen 类常用方法/属性
常用参数说明(适用于 run() 和 Popen())
实例执行命令并捕获输出: 实例
result = subprocess.run(["echo", "Hello"], capture_output=True, text=True) print(result.stdout) # 输出: "Hello\n" 通过 Shell 执行复杂命令: subprocess.run("grep 'error' log.txt | wc -l", shell=True, check=True)
实时获取输出流:
实例
proc = subprocess.Popen(["tail", "-f", "log.txt"], stdout=subprocess.PIPE) while True: line = proc.stdout.readline() if not line: break print(line.decode().strip()) 超时控制: 实例
try: subprocess.run(["sleep", "10"], timeout=5) except subprocess.TimeoutExpired: print("命令超时!")
AI 思考中...
点我分享笔记取消
分类导航
Advertisement
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |