Last active
January 19, 2025 08:42
-
-
Save Askill/1cecb2ba9e8738c9c71235f8ebc75e74 to your computer and use it in GitHub Desktop.
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
# chatgpt generated | |
# but verified | |
import os | |
import shutil | |
import zipfile | |
import tarfile | |
from pathlib import Path | |
def decompress_files(): | |
# Current directory | |
current_dir = Path.cwd() | |
compressed_dir = current_dir / "compressed" | |
uncompressed_dir = current_dir / "uncompressed" | |
# Create directories if they don't exist | |
compressed_dir.mkdir(exist_ok=True) | |
uncompressed_dir.mkdir(exist_ok=True) | |
# Supported compressed formats | |
supported_extensions = [".zip", ".tar", ".gz", ".bz2", ".xz"] | |
for file in current_dir.iterdir(): | |
if file.is_file() and any(file.name.endswith(ext) for ext in supported_extensions): | |
# Create a folder for the uncompressed content | |
target_folder = uncompressed_dir / file.stem | |
target_folder.mkdir(exist_ok=True) | |
try: | |
# Decompress based on file type | |
if file.suffix == ".zip": | |
with zipfile.ZipFile(file, "r") as z: | |
z.extractall(target_folder) | |
elif file.suffix in [".tar", ".gz", ".bz2", ".xz"]: | |
with tarfile.open(file, "r:*") as t: | |
t.extractall(target_folder) | |
# Move the compressed file to the compressed directory | |
shutil.move(str(file), compressed_dir) | |
print(f"Decompressed and moved: {file.name}") | |
except Exception as e: | |
print(f"Error decompressing {file.name}: {e}") | |
if __name__ == "__main__": | |
decompress_files() |
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 zipfile | |
import tarfile | |
from pathlib import Path | |
def go_deeper(current_dir, supported_extensions): | |
for file in current_dir.iterdir(): | |
if file.is_dir() and "compressed" not in str(file): | |
go_deeper(file, supported_extensions) | |
if file.is_file() and any(file.name.endswith(ext) for ext in supported_extensions): | |
compressed_dir = current_dir / "compressed" | |
uncompressed_dir = current_dir / "uncompressed" | |
compressed_dir.mkdir(exist_ok=True) | |
uncompressed_dir.mkdir(exist_ok=True) | |
target_folder = uncompressed_dir / file.stem | |
target_folder.mkdir(exist_ok=True) | |
try: | |
if file.suffix == ".zip": | |
with zipfile.ZipFile(file, "r") as z: | |
z.extractall(target_folder) | |
elif file.suffix in [".tar", ".gz", ".bz2", ".xz"]: | |
with tarfile.open(file, "r:*") as t: | |
t.extractall(target_folder) | |
shutil.move(str(file), compressed_dir) | |
print(f"Decompressed and moved: {file.name}") | |
except Exception as e: | |
print(f"Error decompressing {file.name}: {e}") | |
def decompress_files(): | |
# set a global path to your3d prints folder that is organized by category | |
# into which you download your compressed stl files | |
# this will iterate trough all sub dirs | |
current_dir = Path("C:/3dprints") | |
supported_extensions = [".zip", ".tar", ".gz", ".bz2", ".xz"] | |
go_deeper(current_dir, supported_extensions) | |
if __name__ == "__main__": | |
decompress_files() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment