Last active
July 26, 2023 13:48
-
-
Save Zebartin/7c9aefbd092963b35c8571477c48226e to your computer and use it in GitHub Desktop.
Python: extract files using 7z
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import shutil | |
import subprocess | |
from pathlib import Path | |
def extract_file(file_path: Path, target_path: Path, password=None, remove_original=False): | |
if isinstance(password, str): | |
password = [password] | |
for pwd in password: | |
cmd = [shutil.which('7z'), 'x', | |
f'-p{pwd}', f'-o{target_path}', file_path] | |
process = subprocess.run(cmd, capture_output=True) | |
if process.returncode == 0: | |
if remove_original: | |
file_path.unlink() | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment