Ways4eu WordPress.com Blog

SPA View of ways4eu.wordpress.com

Sort Files in a Directory by File-Extension using Python

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 shutil

defsort_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 valid
ifnot 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 directory
for filename in os.listdir(directory):
file_path = os.path.join(directory, filename)
# Check if the file is a regular file
if 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 sorted
if file_extension in file_endings:
subfolder = os.path.join(directory, file_extension[1:].upper())
# Create the subfolder if it doesn't exist yet
ifnot 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) + 1
else:
# Increase the count of files not moved
files_not_moved += 1

# Print the number of files moved per file extension
print("Files moved:")
for file_extension, count in files_moved.items():
print(f"{file_extension}: {count}")
# Print the number of files not moved
print(f"Files not moved: {files_not_moved}")
# Get the directory to be sorted
directory = input("Enter the directory path: ")
# Get the file endings to be sorted
file_endings = [x.strip() for x ininput("Enter the file endings to sort (comma-separated): ").split(',')]
# Call the function to sort the files
sort_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.

source https://www.ways4.eu/blog/2023/02/10/sort-files-in-a-directory-by-file-extension-using-python/?utm_source=rss&utm_medium=rss&utm_campaign=sort-files-in-a-directory-by-file-extension-using-python