简体   繁体   English

如何从使用 Python 中的 os.system 库的 cat 命令抑制 output

[英]How do I suppress output from a cat command that uses the os.system library in Python

I have a Python 3 program that uses the os process to run the "cat" command.我有一个 Python 3 程序,它使用 os 进程运行“cat”命令。

This is the code fragment, it wirks great, but all the output ends up on the screeen.这是代码片段,它很棒,但所有 output 最终都出现在屏幕上。

os.system("cat *.txt | tee output.txt") os.system("cat *.txt | tee output.txt")

How can I suppress the output from appearing on the screen?如何抑制 output 出现在屏幕上?

Thanks for any help or suggestions.感谢您的任何帮助或建议。

Avoiding The Problem Altogether完全避免问题

tee writes to stdout. tee写入标准输出。 If you don't want to write to stdout, don't use tee :如果您不想写入标准输出,请不要使用tee

os.system("cat -- *.txt >output.txt")

Redirecting Stdout Away重定向标准输出

If you do want to use tee , you can explicitly redirect away its stdout:如果您确实想使用tee ,您可以显式重定向它的标准输出:

os.system('cat -- *.txt | tee output.txt >/dev/null')

Using subprocess.run With stdout=subprocess.DEVNULL使用subprocess.runstdout=subprocess.DEVNULL

If you don't want to modify the shell command to add a redirection within it, subprocess.run gives you enough power to specify redirections to perform before starting a shell:如果您不想修改 shell 命令以在其中添加重定向,则subprocess.run为您提供足够的权限来指定在启动 shell之前执行的重定向:

import subprocess

subprocess.run('cat -- *.txt | tee output.txt', shell=True,
               stdout=subprocess.DEVNULL)

Using subprocess.run Without tee Or A Shell使用没有teesubprocess.run的 subprocess.run

...but it's more efficient and less prone to security issues if you just don't use a shell at all: ...但是如果您根本不使用 shell,它会更有效且更不容易出现安全问题:

import subprocess, glob

subprocess.run(['cat', '--'] + glob.glob('*.txt'),
               stdout=open('output.txt', 'w'))

Using subprocess.run With Tee, But Without A Shell使用带有 Tee 的subprocess.run ,但没有 Shell

If you're up for not using a shell at all, but your real-world use case unconditionally includes some analogue to tee , the Python standard-library documentation describes how to accomplish this at https://docs.python.org/3/library/subprocess.html#replacing-shell-pipeline , as demonstrated below: If you're up for not using a shell at all, but your real-world use case unconditionally includes some analogue to tee , the Python standard-library documentation describes how to accomplish this at https://docs.python.org/3 /library/subprocess.html#replacing-shell-pipeline ,如下所示:

import subprocess, glob
p1 = subprocess.Popen(['cat', '--'] + glob.glob('*.txt'), stdout=subprocess.PIPE)
p2 = subprocess.Popen(['tee', 'output.txt', stdin=p1.stdout, stdout=subprocess.DEVNULL)
p1.stdout.close()
p2.communicate()

You shall take care about the default shell used by os.system您应该注意 os.system 使用的默认 shell

Try to execute this to check the shell used in background:尝试执行此操作以检查后台使用的 shell:

python3 -c 'import os; os.system("ps $$")'

You can change your code a little bit to force the bash shell and redirect the output to /dev/null:您可以稍微更改代码以强制 bash shell 并将 output 重定向到 /dev/null:

os.system('/bin/bash -c "cat *.txt | tee output.txt &>/dev/null"')

Note that regardless of the technique (you could use subprocess module) the point is the background shell and the output redirection.请注意,无论采用何种技术(您可以使用子进程模块),关键是背景 shell 和 output 重定向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM