In a Windows system, when using Python's subprocess to run 7z.exe, the terminal does not display the compression progress. However, when I run the command directly in the Command Prompt (CMD), I can see the progress,tips:the process is Percentage progress,image as follow.
i alse test comand "ping 192.168.0.1" ,I can get real-time output in the console.
import subprocess
command =r'E:\7-Zip\7z.exe a ArchiveName.7z D:\my7zip_test'
command1 = r'ping 192.168.0.1'
# Use subprocess.Popen to run the command and capture the output
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True, universal_newlines=True)
# Read and print the output while the process is running
while True:
output = process.stdout.readline()
if output == '' and process.poll() is not None:
break
if output:
print(output.strip())
# Get the return code of the process
return_code = process.returncode
if return_code == 0:
print("Compression completed successfully.")
else:
print(f"Compression failed with return code {return_code}")
Who knows why and how to do it?
