Skip to content

Feedback on Code #1

@DerekMaggio

Description

@DerekMaggio

Saw your post on Reddit, figured I would give you some feedback like you asked :)
If you want more feedback, let me know in this issue and we can talk through some stuff.

All around nice work though, this script looks like it would come in handy.

Typing

Some typing would be helpful. This helps to tell another user what the each of the args are for your functions as well as the return type.

For instance:

def set_prefix(file_size):
    """
    Returns a special prefix "s_" if file size is smaller than defined, otherwise returns empty string
    """
    if (file_size < MINIMUM_FILE_SIZE):
        return "s_"
    else:
        return "" 

becomes

def set_prefix(file_size: int) -> str:
    """
    Returns a special prefix "s_" if file size is smaller than defined, otherwise returns empty string
    """
    if (file_size < MINIMUM_FILE_SIZE):
        return "s_"
    else:
        return ""

this tells you (or another reader) the arg file_size is an int and your function returns a string.

Folder/File Path Creation

Python has a nifty built in function os.path.join. You can use this instead of string concatenation. You get 2 benefits with this.

  1. It's easier to read because you do not have to put + '\\' everywhere.
  2. Also, if you were to run this script on something other than Windows (Mac or Linux). File path separators are forward slashes /, not back slashes. This function automatically looks at your system type and will use the correct separators.

For instance

def move_files_to_directory(dir):
    """
    Moves files to newly created directories
    """

    filelist = read_files(DIR)

    if len(filelist) != 0:
        msg = str(ALLOWED_FILE_EXTENSIONS).replace("(", "").replace(")", "").replace(chr(39), "")
        print(f'Files found: {len(filelist)}, allowed file extensions: {msg}')
        logging.info(f'Files found: {len(filelist)}, allowed file extensions: {msg}')

        for f in filelist.items():
            ext = f[0].split('.')[1]
            path_current = DIR + '\\' + f[0]
            path_new = DIR + '\\' + ext + '\\' + f[0]

            create_directory(DIR + "\\" + ext)
            os.rename(path_current, path_new)
    else:
        print('No files found. Exiting..')
        logging.info('No files found. Exiting..')
        exit()

becomes

def move_files_to_directory(dir):
    """
    Moves files to newly created directories
    """

    filelist = read_files(DIR)

    if len(filelist) != 0:
        msg = str(ALLOWED_FILE_EXTENSIONS).replace("(", "").replace(")", "").replace(chr(39), "")
        print(f'Files found: {len(filelist)}, allowed file extensions: {msg}')
        logging.info(f'Files found: {len(filelist)}, allowed file extensions: {msg}')

        for f in filelist.items():
            ext = f[0].split('.')[1]
            path_current = os.path.join(DIR, f[0])
            path_new = os.path.join(DIR, ext, f[0])

            create_directory(os.path,join(DIR, ext))
            os.rename(path_current, path_new)
    else:
        print('No files found. Exiting..')
        logging.info('No files found. Exiting..')
        exit()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions