Question

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?

Was it helpful?

Solution

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

OTHER TIPS

Ultimately, there is no absolute way of knowing. For several reasons:

  • Some file format use simple identifiers, but others don't.
  • For those that don't, the only way is analyzing the behavior of a program able to able the format. If the program can successfully open the file, then it belongs to it.
  • But if not, the file could belong to hundreds of formats you don't have a program to open with.

I'm afraid you will need to be content with a partial answer like the ones you already have.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top