From 27365c64dbfdb203e80621bed39ac2e47326dd3a Mon Sep 17 00:00:00 2001 From: ImAlpha Date: Wed, 13 Sep 2023 02:38:26 -0700 Subject: [PATCH] Updated to exclude origin file and include infinite loop with sleep timer --- .../DownloadSorter/DownloadSorter.py | 44 +++++++++++++++++++ .../DownloadSorter/py/DownloadSorter.py | 29 ------------ 2 files changed, 44 insertions(+), 29 deletions(-) create mode 100644 Python_Scripts/DownloadSorter/DownloadSorter.py delete mode 100644 Python_Scripts/DownloadSorter/py/DownloadSorter.py diff --git a/Python_Scripts/DownloadSorter/DownloadSorter.py b/Python_Scripts/DownloadSorter/DownloadSorter.py new file mode 100644 index 0000000..6e6550c --- /dev/null +++ b/Python_Scripts/DownloadSorter/DownloadSorter.py @@ -0,0 +1,44 @@ +import os +import shutil + +EXCLUDE_FILE = 'DownloadSorter.py' + +def create_dir(path, extensions): + for ext in extensions: + if not os.path.exists(os.path.join(path, ext)): + os.mkdir(os.path.join(path, ext)) + + +def move_files(target_path): + for file in os.listdir(target_path): + if os.path.isfile(os.path.join(target_path, file)) and file != EXCLUDE_FILE: + file_extension = file.split('.')[-1] + shutil.move(os.path.join(target_path, file), os.path.join(target_path, file_extension, file)) + + +def main(): + while True: + # Get current directory and set it as the target path + cwd = os.getcwd() + target_path = os.path.join(cwd) + + # Set up the number of files originally for automation + number_of_files = len(os.listdir(cwd)) + time.sleep(20) + old_number = number_of_files + number_of_files = len(os.listdir(cwd)) + + # If new files detected, organize them + if number_of_files != old_number: + # Get all file extensions present in the directory + extensions = {item.split('.')[-1] for item in os.listdir(target_path) + if os.path.isfile(os.path.join(target_path, item))} + # print(extensions) + + # Create a folder for extension types + create_dir(target_path, extensions) + + # Move the files into the folder + move_files(target_path) + +main() \ No newline at end of file diff --git a/Python_Scripts/DownloadSorter/py/DownloadSorter.py b/Python_Scripts/DownloadSorter/py/DownloadSorter.py deleted file mode 100644 index a73c59c..0000000 --- a/Python_Scripts/DownloadSorter/py/DownloadSorter.py +++ /dev/null @@ -1,29 +0,0 @@ -import os -import shutil - -def create_dir(path, extensions): - for ext in extensions: - if not os.path.exists(os.path.join(path, ext)): - os.mkdir(os.path.join(path, ext)) - - -def move_files(target_path): - for file in os.listdir(target_path): - if os.path.isfile(os.path.join(target_path, file)): - file_extension = file.split('.')[-1] - shutil.move(os.path.join(target_path, file), os.path.join(target_path, file_extension, file)) - -def main(): - cwd = os.getcwd() - target_path = os.path.join(cwd) - extensions = {item.split('.')[-1] for item in os.listdir(target_path) if os.path.isfile(os.path.join(target_path, item))} - # print(extensions) - - # Create a folder for extension types - create_dir(target_path, extensions) - - # Move the files into the folder - move_files(target_path) - - -main() \ No newline at end of file