通过Python静默运行7zip命令行 [英] Running 7zip command line silently via Python
问题描述
我已经看到了很多有关使用popen对.exe文件执行python的问题,并提到了使用PIPE停止进程输出的问题.抱歉,如果我的术语不正确,我是python的新手.
I've seen plenty of questions regarding the python execution of a .exe file using popen and mentions of using PIPE to stop output of the process. Apologies if my terminology is incorrect, i'm very new to python.
这个问题的主要目的是添加stdout = PIPE或类似的东西以防止显示任何输出,例如
My main aim of this question to to add stdout=PIPE or something similar to prevent any output showing, such as
正在提取文件名...",这非常糟糕,因为某些rar文件很大.
"Extracting filename..." This is very bad as some rars are large.
我正在尝试静默/隐藏/运行7zip.这样整个过程可以在后台运行,并且不会干扰当前的屏幕操作.
I am trying to run 7zip silently/hidden/quite. So that the entire process can run in the background and not interfere with current on screen operations.
这时,当前脚本可以正常工作.
At this time, the current script works fine.
这是我的python代码: Python 2.7
Here is my python code: Python 2.7
用于执行代码的Pythonw.exe:
Pythonw.exe used to execute code:
pythonw.exe script.py
pythonw.exe script.py
if ".rar" in input_file:
subprocess.Popen("C:\\Program Files (x86)\\7-Zip\\7z e " + input_file + " -o" + output_dest + " -y")
ALl的帮助表示感谢,谢谢.
ALl help appreciated, thanks.
推荐答案
将stdout和stderr插入系统的空文件:
Pipe stdout and stderr to your system's null file:
import os
with open(os.devnull, 'w') as null:
subprocess.Popen(['7z', 'e', input_file, '-o', output_dest, '-y'], stdout=null, stderr=null)
这篇关于通过Python静默运行7zip命令行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!