Last active
November 5, 2023 20:57
-
-
Save yfwu/12d4c13ac2a759acb066dc00316d7405 to your computer and use it in GitHub Desktop.
Organize the nested folders to move all video files from subfolders into the base folder.
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
#!/bin/env python3 | |
# %% | |
import os | |
from pathlib import Path | |
import sys | |
# %% | |
# get all the folders and nested folders and files into a list | |
def get_all_files(path): | |
"""Get all files in a folder and subfolders""" | |
all_files = [] | |
for root, dirs, files in os.walk(path): | |
for file in files: | |
all_files.append(os.path.join(root, file)) | |
return all_files | |
# %% | |
# delete files that are not vedio files and smaller than 1 GB | |
def delete_files(path): | |
"""Delete files that are not video files and smaller than 1 GB""" | |
all_files = get_all_files(path) | |
for file in all_files: | |
if not file.endswith((".mp4", ".mkv", ".avi", ".mov")): | |
os.remove(file) | |
elif os.path.getsize(file) < 1000000000: | |
os.remove(file) | |
# %% | |
# move the files in the nested folders to current folder | |
def move_files(path): | |
"""Move files in nested folders to current folder""" | |
all_files = get_all_files(path) | |
for file in all_files: | |
if os.path.isfile(file): | |
os.rename(file, os.path.join(path, os.path.basename(file))) | |
# %% | |
# delete all folders | |
def delete_folders(path): | |
"""Delete all folders""" | |
all_folders = get_all_files(path) | |
for folder in all_folders: | |
if os.path.isdir(folder): | |
os.rmdir(folder) | |
# %% | |
# set the path as sys.argv[1] or current working directory | |
# execute the program | |
if __name__ == "__main__": | |
if len(sys.argv) > 1: | |
path = Path(sys.argv[1]) | |
else: | |
path = Path.cwd() | |
delete_files(path) | |
move_files(path) | |
delete_folders(path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your script provides a sequence of operations intended to organize a directory by deleting non-video files and files smaller than 1 GB, then moving all remaining files to the current directory, and finally deleting all subdirectories. However, there are a few issues and considerations that you may want to address before running this script: