0

I am looking for a function in Python to determine the type of a file. I mean, the input is the path where the file is located, and the output is the type.

Thanks for your help

David Jones
  • 4,766
  • 3
  • 32
  • 45

2 Answers2

0

If looking at the extension is enough, mimetypes.guess_type() is your friend.

For more robust identification, you might want to look at python-magic.

AKX
  • 152,115
  • 15
  • 115
  • 172
0

The Python Magic library provides the functionality you need.

You can install the library with pip install python-magic and use it as follows:

>>> import magic
>>> magic.from_file('filename')
AChampion
  • 29,683
  • 4
  • 59
  • 75
jozalex
  • 21
  • 2