在CentOS 7中使用Python 3执行系统命令

1. 使用os.system()

这个方法简单直接,但它不返回命令的输出,只返回命令的退出状态。如果你只需要知道命令是否成功执行,这个方法就足够了。

python 复制代码
import os

cmd = "ls -l"
status = os.system(cmd)

if status == 0:
    print("Command executed successfully")
else:
    print("Command execution failed")

2. 使用subprocess.run()

这是从Python 3.5开始推荐的方式,它提供了更多的功能和灵活性。特别是,它允许你捕获命令的输出。

python 复制代码
import subprocess

try:
    result = subprocess.run(["ls", "-l"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
    print("stdout:", result.stdout)
except subprocess.CalledProcessError as e:
    print("Error executing command:", e)

3. 使用subprocess.Popen()

当你需要更细粒度的控制,比如非阻塞读取输出或写入输入到进程,subprocess.Popen()是一个更复杂但更强大的选择。

python 复制代码
import subprocess

process = subprocess.Popen(["ls", "-l"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
stdout, stderr = process.communicate()

print("stdout:", stdout)
if process.returncode != 0:
    print("stderr:", stderr)

注意事项

  • 在CentOS 7上,默认可能不会安装Python 3。你可能需要手动安装Python 3及其pip包管理器。
  • 当执行需要特定权限的命令时(例如,操作系统级别的任务),确保你的Python脚本以合适的用户权限运行。
  • 对于一些复杂的命令,特别是那些涉及管道(|)、重定向(><)等Shell特性的命令,可能需要通过shell=True参数传递给subprocess.run()subprocess.Popen(),或者将命令作为一个字符串而不是列表传递。但要小心使用shell=True,因为它可能会引入安全风险,特别是当命令字符串来自不可信的输入时。

在使用这些方法时,请确保你的Python脚本考虑到了CentOS 7环境的特点,包括任何潜在的路径和权限问题。

相关推荐
helloweilei9 小时前
python 抽象基类
python
用户8356290780519 小时前
Python 实现 PPT 转 HTML
后端·python
zone773915 小时前
004:RAG 入门-LangChain读取PDF
后端·python·面试
zone773915 小时前
005:RAG 入门-LangChain读取表格数据
后端·python·agent
树獭非懒1 天前
AI大模型小白手册|Embedding 与向量数据库
后端·python·llm
唐叔在学习1 天前
就算没有服务器,我照样能够同步数据
后端·python·程序员
曲幽1 天前
FastAPI流式输出实战与避坑指南:让AI像人一样“边想边说”
python·ai·fastapi·web·stream·chat·async·generator·ollama
Flittly1 天前
【从零手写 AI Agent:learn-claude-code 项目实战笔记】(1)The Agent Loop (智能体循环)
python·agent
vivo互联网技术1 天前
ICLR2026 | 视频虚化新突破!Any-to-Bokeh 一键生成电影感连贯效果
人工智能·python·深度学习