Skip to content

Instantly share code, notes, and snippets.

@AlexGascon
Last active June 20, 2020 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexGascon/dfc6117c9fcbdb1b991f9d7cec7b2aa1 to your computer and use it in GitHub Desktop.
Save AlexGascon/dfc6117c9fcbdb1b991f9d7cec7b2aa1 to your computer and use it in GitHub Desktop.
Python script to uncompress all the rar files in the current directory and delete the compressed .rar afterwards. It uses several system subprocesses in parallel to speed-up the process. Also valid if the files are protected with a password. Requires the rarfile (https://pypi.org/project/rarfile/) package
import glob
import multiprocessing
import os
import rarfile
NUM_PROCESSES = 5
PASSWORD = None # Set to a string if the rar files are protected with a password
def log(msg):
print(msg)
def list_files(pattern):
return glob.glob(f"./{pattern}")
def extract_rar(filename):
log(f"Extracting {filename}...")
rar = rarfile.RarFile(filename)
rar.extractall(pwd=PASSWORD)
log(f"Extraction completed!")
def delete_file(filename):
log(f"Deleting file {filename}...")
os.remove(filename)
log(f"<Not actually deleting, just testing this for the moment>")
def process_file(filename):
try:
extract_rar(filename)
delete_file(filename)
except Exception as e:
log("********************************************************")
log(f"ERROR WHEN PROCESSING {filename}")
log(e)
log("********************************************************")
rar_files = list_files('*.rar')
with multiprocessing.Pool(NUM_PROCESSES) as p:
p.map(process_file, rar_files)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment