Running 7zip command line silently via Python

2019-09-15 02:36发布

问题:

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.

My main aim of this question to to add stdout=PIPE or something similar to prevent any output showing, such as

"Extracting filename..." This is very bad as some rars are large.

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.

Here is my python code: Python 2.7

Pythonw.exe used to execute code:

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 help appreciated, thanks.

回答1:

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)