Skip to content

Commit

Permalink
new project
Browse files Browse the repository at this point in the history
  • Loading branch information
Federico committed Aug 2, 2023
1 parent f090bbb commit 8ddae53
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
77 changes: 77 additions & 0 deletions intermediate_projects/file_sorter/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import os
import shutil


# BONUS: Create a tkinter app that allows the user to select the file via a button

def create_folder(path: str, extension: str) -> str:
"""Creates a folder that is named after the extension of the file passed in"""

# Create the folder name excluding the dot "."
folder_name: str = extension[1:]

# Create a valid path with the new folder name
folder_path: str = os.path.join(path, folder_name)

# If the folder path doesn't exist yet, create it
if not os.path.exists(folder_path):
os.makedirs(folder_path)

# Return the new path
return folder_path


def sort_files(source_path: str):
"""Sorts files based on a given path"""

# Recursively walk through all the directories and files in the source path
for root_dir, sub_dir, filenames in os.walk(source_path):

# Get the full path for each file
for filename in filenames:
file_path: str = os.path.join(root_dir, filename)

# Splits the path into a root and the extension
extension: str = os.path.splitext(filename)[1]

# Check that there is an extension and create a folder if there is
if extension:
target_folder: str = create_folder(source_path, extension)

# Create a path that leads to the target folder
target_path: str = os.path.join(target_folder, filename)

# Move the file from its current location to the target folder
shutil.move(file_path, target_path)


def remove_empty_folders(path):
"""Removes all empty folders"""

# We walk through all our folders again, but this time in a bottom-up approach
for root_dir, sub_dir, filenames in os.walk(path, topdown=False):

# Create a valid folder path for each dir
for current_dir in sub_dir:
folder_path: str = os.path.join(root_dir, current_dir)

# Check if a folder is empty, and removes it if it is
if not os.listdir(folder_path):
os.rmdir(folder_path)


def main():
# Get some user input
user_input: str = input('Please provide a file path to sort: ')

# Check if the path the user provided exists
if os.path.exists(path=user_input):
sort_files(user_input)
remove_empty_folders(user_input)
print('Files sorted successfully!')
else:
print('Invalid path, please provide a valid file path.')


if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion intermediate_projects/pdf_reader/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self):
self.WHITE: tuple = (255, 255, 255)
self.BLACK: tuple = (0, 0, 0)
self.RED: tuple = (255, 99, 71)
self.BLUE: tuple = (65,105,225)
self.BLUE: tuple = (65, 105, 225)

# Font
default_font: str = pygame.font.get_default_font()
Expand Down

0 comments on commit 8ddae53

Please sign in to comment.