Skip to content

Commit

Permalink
Merge pull request #15 from rjwignar/issue-13-2
Browse files Browse the repository at this point in the history
Refactor Default Routine
  • Loading branch information
rjwignar authored May 18, 2024
2 parents f8a549d + 6a07e2a commit f3cdec2
Showing 1 changed file with 59 additions and 87 deletions.
146 changes: 59 additions & 87 deletions folderizer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
import argparse
import os

def createFolder(targetPath, folderName):
print(folderName + " type detected")
folderPath = os.path.join(targetPath, folderName)
print(folderPath)
if not (os.access(folderPath, os.F_OK)):
print('creating images folder')
os.mkdir(folderPath)
else:
print(folderPath + " already created")
return folderPath

fileCategories = {}
def addFileCategory(categoryName, extensions):
fileCategories[categoryName] = {"folderName": f"{categoryName}", "extensions" : extensions, "fileCount" : 0, "movedCount" : 0}

def dictionarySummary(dictionary):
print('------------------------------------')
print('SUMMARY')
print('------------------------------------')
for entry, properties in dictionary.items():
print(f"{entry} found: {properties['fileCount']}")
print(f"{entry} successfully moved: {properties['movedCount']}")

def main():
images = [".jpeg", ".jpg", ".png", ".gif"]
videos = [".mp4", ".flv", ".m4v", ".webm"]
text = [ ".txt", ".md", ".doc", ".docx", ".pdf"]
imageCount = 0
movedImagesCount = 0
videoCount = 0
movedVideosCount = 0
textCount = 0
movedTextFilesCount = 0
# Define extensions
imageExtensions = [".jpeg", ".jpg", ".png", ".gif"]
videoExtensions = [".mp4", ".flv", ".m4v", ".webm"]
textExtensions = [ ".txt", ".md", ".doc", ".docx", ".pdf"]

# Populate fileCategories with category and extensions
addFileCategory("images", imageExtensions)
addFileCategory("videos", videoExtensions)
addFileCategory("textFiles", textExtensions)

# Initialize parser
parser = argparse.ArgumentParser(
prog="folderizer",
description="File organization command-line tool.",
Expand All @@ -21,92 +45,40 @@ def main():
parser.add_argument("-d", "--default", action='store_true', help="Sorts files into folders by category (image, video, text, code, etc)")
args = parser.parse_args()
print(args, args.filepath, args.default)
if args.default:
print("Default behaviour")
print(os.path.abspath(args.filepath))

# --default subroutine
def defaultBehavior():
# Extract target path from arguments
target_path = os.path.abspath(args.filepath)
print("Target Path: " + target_path)
# Check if filepath exists
if (os.access(target_path, os.F_OK)):
print("Filepath exists")
print(os.scandir(target_path))
with os.scandir(target_path) as it:
# Iterate through each non-hidden file in target_path
for entry in it:
if not entry.name.startswith('.') and entry.is_file():
print("Entry name: " + entry.name)
print(os.path.splitext(entry.name)[1])
if os.path.splitext(entry.name)[1] in images:
imageCount+=1
print("image detected")
imagePath = os.path.join(target_path, "images")
print(imagePath)
if not (os.access(imagePath, os.F_OK)):
print('creating images folder')
os.mkdir(imagePath)
else:
print("not creating images folder")
# move file to images folder
print("Image file name: " + entry.name)
print(entry)
oldPath = target_path + "\\" + entry.name
newPath = imagePath + "\\" + entry.name
print("old filepath: " + oldPath)
print("new filepath: " + newPath)
# attempt rename
os.rename(oldPath, newPath)
movedImagesCount+=1
elif os.path.splitext(entry.name)[1] in videos:
videoCount+=1
print("video detected")
videoPath = os.path.join(target_path, "videos")
print(videoPath)
if not (os.access(videoPath, os.F_OK)):
print('creating videos folder!')
os.mkdir(videoPath)
else:
print("not creating videos folder")
# move file to videos folder
print("Video file name: " + entry.name)
print(entry)
oldPath = target_path + "\\" + entry.name
newPath = videoPath + "\\" + entry.name
print("old filepath: " + oldPath)
print("new filepath: " + newPath)
# attempt rename
os.rename(oldPath, newPath)
movedVideosCount+=1
elif os.path.splitext(entry.name)[1] in text:
textCount+=1
print("text file detected")
textPath = os.path.join(target_path, "textFiles")
print(textPath)
if not (os.access(textPath, os.F_OK)):
print('creating textFiles folder!')
os.mkdir(textPath)
else:
print("not creating textFiles folder")
# move file to videos folder
print("Text file name: " + entry.name)
print(entry)
oldPath = target_path + "\\" + entry.name
newPath = textPath + "\\" + entry.name
print("old filepath: " + oldPath)
print("new filepath: " + newPath)
# attempt rename
os.rename(oldPath, newPath)
movedTextFilesCount+=1
print('------------------------------------')
print('SUMMARY')
print('------------------------------------')
print('Images found: ',imageCount)
print('Images successfully moved: ', movedImagesCount)
print('Videos found: ',videoCount)
print('Videos successfully moved: ', movedVideosCount)
print('Text files found: ',textCount)
print('Text files successfully moved: ', movedTextFilesCount)
# Extract file extension from entry
fileExtension = os.path.splitext(entry.name)[1]
# Search for extension match in fileCategories
for fileType, properties in fileCategories.items():
if fileExtension in properties['extensions']:
properties['fileCount'] +=1
createFolder(target_path, fileType)
oldPath = os.path.join(target_path, entry.name)
newPath = os.path.join(target_path, fileType, entry.name)
# Move file into appropriate folder
os.rename(oldPath, newPath)
properties['movedCount'] +=1
# Display file(s) found and file(s) moved by fileCategory
dictionarySummary(fileCategories)
else:
print("Filepath doesn't exist")

# --default
if args.default:
defaultBehavior()

def example_function():
return 1 + 1

if __name__ == "__main__":
main()
main()

0 comments on commit f3cdec2

Please sign in to comment.