小编num*_*623的帖子

Python - 运行 shell 命令,获取 stdout 和 stderr 作为变量,但对用户隐藏?

我想让我的 Python 脚本运行 Linux shell 命令并将输出存储在变量中,而不向用户显示命令的输出。我已经尝试过这个os.systemsubprocess.check_outputsubprocess.runsubprocess.Popen没有os.popen运气

我当前的方法正在运行os.system("ls -l &> /tmp/test_file"),因此命令 stdout 和 stderr 通过管道传输到/tmp/test_file,然后我让我的 Python 代码将文件读取到变量中,然后将其删除。

有没有更好的方法来做到这一点,以便我可以将命令输出直接发送到变量中,而不必创建和删除文件,但对用户隐藏它?

python subprocess capture python-3.x

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

capture ×1

python ×1

python-3.x ×1

subprocess ×1