on Fri Feb 10 2023
Here is a python script that sorts files in a directory by file extension.
The directory to be sorted and the file endings to be sorted are taken as input parameters.
As an example, the script can be immensely helpful in creating order in a download folder that has grown and grown over time.
Read more: Sort Files in a Directory by File-Extension using Python
This script first checks if the specified directory is valid. If it is, it iterates through all the files in the directory and checks their file extensions. If the file extension is one of the file endings specified in the input parameters, the file is moved to a sub-folder named after the file extension. The subfolder is created if it doesn’t exist yet.
import osimport shutildefsort_files_by_extension(directory, file_endings):""" Sorts the files in the specified directory by file extension.
Arguments: - directory: The path of the directory to be sorted - file_endings: A list of file endings to be sorted
"""
# Check if the directory is validifnot os.path.isdir(directory):print(f"{directory} is not a valid directory.")return
# Keep track of the number of files moved per file extension files_moved = {}# Keep track of the number of files not moved in the directory files_not_moved = 0
# Iterate through all the files in the directoryfor filename in os.listdir(directory): file_path = os.path.join(directory, filename)# Check if the file is a regular fileif os.path.isfile(file_path): file_extension = os.path.splitext(filename)[1]# Check if the file extension is one of the file endings to be sortedif file_extension in file_endings: subfolder = os.path.join(directory, file_extension[1:].upper())# Create the subfolder if it doesn't exist yetifnot os.path.exists(subfolder): os.makedirs(subfolder)# Move the file to the subfolder shutil.move(file_path, os.path.join(subfolder, filename))# Increase the count of files moved for this file extension files_moved[file_extension] = files_moved.get(file_extension, 0) + 1else:# Increase the count of files not moved files_not_moved += 1
# Print the number of files moved per file extensionprint("Files moved:")for file_extension, count in files_moved.items():print(f"{file_extension}: {count}")# Print the number of files not movedprint(f"Files not moved: {files_not_moved}")# Get the directory to be sorteddirectory = input("Enter the directory path: ")# Get the file endings to be sortedfile_endings = [x.strip() for x ininput("Enter the file endings to sort (comma-separated): ").split(',')]# Call the function to sort the filessort_files_by_extension(directory, file_endings)
This script keeps track of the number of files moved for each file extension in the files_moved dictionary. It also keeps track of the number of files not moved in the directory in the files_not_moved variable. At the end of the script, it prints out the number of files moved per file extension and the number of files not moved in the directory.
You can download the sourcecode from my github repository:
https://github.com/smartDevel/fileSorter
The post Sort Files in a Directory by File-Extension using Python appeared first on Strictly Confidential.