Search code examples
pythonlinuxfile-extensionfile-type

File extension validation with python


I want to check if a given file's extension is correct or not. For example, someone give me a file with an extension .zip but actually it may be an executable.

Using mimetypes I could not determine a file's real type. As far as I see, mimetypes needs an extension.

I can map the output of unix file command with some extensions. Even if you change the extension, you cannot deceive file command. However, this solution needs a subprocess.

I thought, there may be a more pythonic solution of this problem. Does anyone know?


Solution

  • Searching the name of the C library (libmagic) used for the file command, nets 3 interesting python packages on PyPI: