Skip to content

Instantly share code, notes, and snippets.

@yfwu
Last active November 5, 2023 20:57
Show Gist options
  • Save yfwu/12d4c13ac2a759acb066dc00316d7405 to your computer and use it in GitHub Desktop.
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.
#!/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)
@mikurdi
Copy link

mikurdi commented Nov 5, 2023

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:

Dangerous Operations: The script performs deletions and moves without any confirmation or checks if something went wrong. This can potentially lead to data loss.

Deletion Logic: Your deletion logic in delete_files checks if a file is not a video or is smaller than 1 GB and then deletes it. However, this means that it will delete video files that are smaller than 1 GB as well. The logic should be adjusted to make sure that all video files are kept regardless of their size.

Moving Files: The move_files function will move all files to the specified path, but if a file with the same name already exists, it will raise an exception. You need to handle this case, perhaps by renaming duplicates or skipping the move.

Deleting Folders: The delete_folders function actually gets a list of all files, not folders. To remove directories, you should first make sure they are empty; otherwise, os.rmdir will fail. You would also need to collect a list of directories, not files, and it is more efficient to do this in reverse order so that subdirectories are deleted before their parent directories.

Pathlib Integration: Since you're importing Path from pathlib, it might be more consistent to use Path objects throughout your script instead of mixing os.path and Path.

Exception Handling: None of the functions include exception handling. This is critical for operations that alter the file system, as many issues can arise (e.g., permission errors, non-existent files, etc.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment