alexlostorto / file-manipulation

Create, delete and edit files. Merge PDFs too!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

File Manipulation

Create, edit and delete files with ease!

Maintainability

# List of scripts
move-files.py
organise-files.py
remove-dupes.py
remove-files.py
pdf-merger.py

πŸ“” Table of Contents

Click to expand

πŸ”§ Functions

yesNo(question):

Takes a question as a parameter and prompts the user to input yes or no. This function returns either True for yes or False for no.

def yesNo(question):
    userInput = input(question)
    print()

    while True:
        # Check input is acceptable
        if userInput == '':
            print(f"Don't leave me empty!")
        elif userInput.lower() in ['y', 'ye', 'yes', 'yeah']:
            return True
        elif userInput.lower() in ['n', 'no', 'nop', 'nope']:
            return False

getInput(array, question):

Takes an array of options and a question as parameters. This function numbers and logs all the options then prompts the user to make a choice. The option chosen by the user is returned as an integer value.

def getInput(array, question):
    userInput = input(question)
    print()

    while True:
        # Check input is acceptable
        if userInput == '':
            return False
        elif not userInput.isdigit() or int(userInput) < 1 or int(userInput) > len(array):
            print(f"Input has to be a number 1-{len(array)}")
            userInput = input(question)
            continue
        else:
            # Chooses file
            userInput = int(userInput)
            return userInput

checkName(fileName):

Takes a file name as a parameter. This function checks if the global USE_PREFIX and USE_SUFFIX variables are True and if they are, then the file name is checked to see if it contains the corresponding PREFIX/SUFFIX. Returns True if the file name matches all criteria.

def checkName(fileName):
    fileName = str(fileName)
    if not USE_PREFIX and not USE_SUFFIX:
        return True
    elif USE_PREFIX and fileName.startswith(PREFIX) and USE_SUFFIX and fileName.endswith(SUFFIX):
        return True
    elif USE_PREFIX and fileName.startswith(PREFIX) and not USE_SUFFIX:
        return True
    elif USE_SUFFIX and fileName.endswith(SUFFIX) and not USE_PREFIX:
        return True
    else:
        return False

traverseFiles(root, question):

Takes the root directory and a question as parameters. This function allows the user to traverse through the device's filesystem and returns the chosen directory.

def traverseFiles(root, question):
    while True:
        dirs = [d for d in os.listdir(root) if os.path.isdir(os.path.join(root, d))]

        if len(dirs) == 0:
            return root

        # Choose directory
        for i in range(len(dirs)):
            print(f"{i+1}) {dirs[i]}")

        dir = getInput(dirs, question)

        if not dir:
            return root

        print(f"You chose '{dirs[dir-1]}'\n")
        root = os.path.join(root, dirs[dir-1])

πŸ“‹ Scripts

Remove Dupes

  1. Lists all files in the specified directory.
files = [f for f in os.listdir(ROOT) if os.path.isfile(os.path.join(path, f))]
  1. If the file matches the regex (e.g. (1) or (321)), and the file already exists in the directory, it is appended to the dupFiles list.
dupFiles = []
for file in files:
    position = re.search('\(\d+\).\w+', file)
    fileNameCut = re.sub('\(\d+\)', '', file)
    if position is not None and fileNameCut in files:
        dupFiles.append(file)
  1. If the user confirms the deletion of the files, use os.remove() to delete each file in dupFiles.
if yesNo("Delete files (y/n): "):
    for file in dupFiles:
        os.remove(os.path.join(ROOT, file))

Remove Files

  1. Lists all files in the specified directory and add it to the list if it matches the file name criteria.
files = [f for f in os.listdir(ROOT) if os.path.isfile(os.path.join(path, f)) if checkName(f)]
  1. If the user confirms the deletion of the files, use os.remove() to delete each file in files.
if yesNo("Delete files (y/n): "):
    for file in files:
        os.remove(os.path.join(ROOT, file))

Move Files

  1. Lists all files in the specified directory and add it to the list if it matches the file name criteria.
files = [f for f in os.listdir(ROOT) if os.path.isfile(os.path.join(path, f)) if checkName(f)]
  1. If the user confirms the moving of the files, use shutil.move() to move each file into the destination directory.
if yesNo("Move files (y/n): "):
    for file in files:
        shutil.move(os.path.join(source, file), os.path.join(destination, file))

πŸ“œ Credits

Everything is coded by Alex lo Storto

Licensed under the MIT License.

About

Create, delete and edit files. Merge PDFs too!

License:MIT License


Languages

Language:Python 100.0%