Created
July 17, 2024 19:09
-
-
Save wall-e-08/663e92b6bfeb6d7a7213de0c6d88e762 to your computer and use it in GitHub Desktop.
Zip subfolder to keep backup easily ignoring sub-subfolders
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 os | |
import zipfile | |
def zip_project_subfolders(directory): | |
# Sub-subfolders to ignore | |
ignore_folders = {'.idea', '.git', 'env', 'node_modules'} | |
for item in os.listdir(directory): | |
item_path = os.path.join(directory, item) | |
if os.path.isdir(item_path) and item != 'the_project_you_dont_want_to_keep': | |
zip_filename = f"{item}.zip" | |
with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf: | |
for root, dirs, files in os.walk(item_path): | |
# Skip ignored folders | |
dirs[:] = [d for d in dirs if d not in ignore_folders] | |
for file in files: | |
file_path = os.path.join(root, file) | |
relative_path = os.path.relpath(file_path, directory) | |
zipf.write(file_path, relative_path) | |
# print(f"zip write: {file_path=}, {relative_path=}") | |
print(f'Zipped {item_path} into {zip_filename}') | |
if __name__ == "__main__": | |
zip_project_subfolders('/path/to/your/works') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment