DEV Community

Sérgio Araújo
Sérgio Araújo

Posted on

Guessing filetypes in python

Where this idea came from?

If you want to build an application to automatically organize your files, the best approach is to automate the mime type guessing, because manually creating list of file types is counter-productive, at least you have to create generic groups, like multimedia, images, docs, etc, each group will have their subgroups defined by the app, for example:

Audio: ['mp3', 'wma', 'm4a'] 
Image: ['jpg', 'png', 'svg']
Enter fullscreen mode Exit fullscreen mode

This article will change, because I am still building it

But the initial code is here:

import mimetypes
import os

for f in os.listdir('.'):
    filetype, _ = mimetypes.guess_type(f)
    filetype = str(filetype).split('/')[0]
    if not filetype == "None":
        print(f'{f} {filetype}')
Enter fullscreen mode Exit fullscreen mode

The mimetypes.guess_type() returns a tuple, so you can manipulate the result to fit your needs.

Later I discovered that mimetypes is not enough, see here then I installed

pip3 install python-magic
Enter fullscreen mode Exit fullscreen mode

Then I figured out this soution:

import magic
from pathlib import Path
target = Path.home() / 'music'

def file_path_mime(file_path):
    mime = magic.from_file(file_path, mime=True)
    return mime

# The check_in_memory_mime function works for Django 
# file uploads pulled directly from request.FILES on POST.
def check_in_memory_mime(in_memory_file):
    mime = magic.from_buffer(in_memory_file.read(), mime=True)
    return mime

for f in target.iterdir():
    if f.is_file():
        filetype = file_path_mime(str(f.resolve()))
        print(f'{f.name} {filetype}')
Enter fullscreen mode Exit fullscreen mode

The initial file organizer, still with a generic file organization

It enters you "Downloads" folder and creates a folder for each file extension on it, like zip_files. Then it will get all files from this type and put them into the folder.

from pathlib import Path

downloads = Path.home() / 'Downloads/'

for f in downloads.iterdir():
    if f.is_file():
        name, ext = f.name, f.suffix[1:]
        p = Path(downloads) / f'{ext}_files'
        p.mkdir(parents=True, exist_ok=True)
        Path(f).rename(p / f'{f.name}')
Enter fullscreen mode Exit fullscreen mode

Improvements

If you have any idea to better combine these two codes in a very pyctonic way, feel free to suggest new versions, you can maybe share your oun version of these ideas.

All of this started after reading this article.

Discussion (0)