Last active
March 4, 2024 13:47
-
-
Save notalentgeek/25438aa0e60dbb26bd8696779514a5ab to your computer and use it in GitHub Desktop.
My Python script to sort file based on its MIME type into separate folders.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a Python script that let you arrange | |
# files into a folder for each extension. Actually | |
# I lied, this script is not about file extension | |
# but about a file's mime type. So, in case there | |
# are files that has no extension this script can | |
# predict what the file's extension could be. | |
# However, the only requirement is that file need | |
# to be able to be read by the computer's OS. For | |
# example image files usually has no problem with | |
# this script also with other media files. But, | |
# uncommon file type will have a problem. | |
# Especially, if the computer does not how to open | |
# the files.THIS IS A PYTHON3 CODES. PYTHON MAGIC | |
# LIBRARY DOES NOT WORKING WITH PYTHON2. | |
import magic | |
import os | |
import shutil | |
import sys | |
def ArrangeFilesBasedOnFileType(_startDir): | |
print("Only works with Python 3.") | |
# First thing is that I need to get | |
# list of all files. Do not forget | |
# to remove the folders. | |
startDirList = os.listdir(_startDir) | |
startDirListWithoutFolder = [] | |
# Remove the folders!!!! | |
for fileOrFolder in startDirList: | |
# When sorting things do not forget | |
# to put it as an absolute path. | |
absolutePathLOLIForgetToPutThisOneXDXDXD = ( | |
os.path.join(_startDir, fileOrFolder)) | |
if not os.path.isdir( | |
absolutePathLOLIForgetToPutThisOneXDXDXD): | |
#print(fileOrFolder) | |
startDirListWithoutFolder.append( | |
absolutePathLOLIForgetToPutThisOneXDXDXD) | |
for fileAbsPath in startDirListWithoutFolder: | |
#print(file) | |
# Checking fileAbsPath type not based from extension. | |
# The checking from the Python Magic | |
fileType = magic.from_file(fileAbsPath, mime=True) | |
#print(fileType) | |
thisIsReallyTheFileTypeLOL = fileType.split("/")[0] | |
possibleExtension = fileType.split("/")[1] | |
# Create the folder name without any special character. | |
# the isalnum() is to detect is a char/string is a alphabet | |
# or a number. | |
folderName = ''.join( | |
s for s in possibleExtension if s.isalnum()) | |
#print(thisIsReallyTheFileTypeLOL) | |
#print(possibleExtension) | |
# Construct the folder name with absolute | |
# path obviously. | |
absPathIntoTheFolderWeWantToCheck = ( | |
os.path.join(_startDir, folderName)) | |
#print(absPathIntoTheFolderWeWantToCheck) | |
# Check if the abs path to folder is exist or | |
# not a directory. If either then we create the | |
# folder. | |
if (not os.path.exists(absPathIntoTheFolderWeWantToCheck) or | |
not os.path.isdir(absPathIntoTheFolderWeWantToCheck)): | |
os.mkdir(absPathIntoTheFolderWeWantToCheck) | |
# Take the last name of the absolute path. | |
justTheFileName = os.path.basename( | |
os.path.normpath(fileAbsPath)) | |
#print(justTheFileName) | |
# Create the absolute path into the new file. | |
fileNewAbsPath = os.path.join( | |
absPathIntoTheFolderWeWantToCheck, justTheFileName) | |
#print(absPathIntoTheFolderWeWantToCheck) | |
#print(fileAbsPath) | |
#print(fileNewAbsPath) | |
# Finally! We move it there! | |
shutil.move(fileAbsPath, fileNewAbsPath) | |
if __name__ == "__main__": | |
ArrangeFilesBasedOnFileType(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment