chatgpt-4o-latest-20250326· OpenAI
LMArena Human Preference 140K
我想使用python运行命令行
- User
我想使用python运行命令行
- Assistant
在 Python 中运行命令行命令,常用的方式有以下几种:
✅ 使用
subprocess模块(推荐)subprocess是 Python 官方推荐用来执行外部命令的模块,功能强大,可以替代旧的os.system()。🌟 示例 1:基本用法
import subprocess # 运行一个命令(例如 Windows 下),获取输出 result = subprocess.run(['dir'], shell=True, capture_output=True, text=True) print(result.stdout)🌟 示例 2:Linux / macOS 上运行
ls -laimport subprocess result = subprocess.run(['ls', '-la'], capture_output=True, text=True) print(result.stdout)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.