sorter improved for current working directory, needs line to be skipped

This commit is contained in:
Jacob Delgado 2023-09-13 00:58:54 -07:00
parent d74ab90aa9
commit cf444b3ca3
5 changed files with 29 additions and 17 deletions

View File

@ -1,16 +0,0 @@
import os
import shutil
target_path = './TestFiles'
extensions = {item.split('.')[-1] for item in os.listdir(target_path) if os.path.isfile(os.path.join(target_path, item))}
# print(extensions)
for ext in extensions: # Create a folder for extension types
if not os.path.exists(os.path.join(target_path, ext)):
os.mkdir(os.path.join(target_path, ext))
# Move the files into the folder
for item in os.listdir(target_path):
if os.path.isfile(os.path.join(target_path, item)):
file_extension = item.split('.')[-1]
shutil.move(os.path.join(target_path, item), os.path.join(target_path, file_extension, item))

View File

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

@ -0,0 +1,29 @@
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()

View File

@ -81,7 +81,6 @@ def run_command(command, path):
os.chdir(cwd) os.chdir(cwd)
def main(source, target): def main(source, target):
# Get full folder path to avoid bugs in differing OS # Get full folder path to avoid bugs in differing OS
cwd = os.getcwd() cwd = os.getcwd()