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

2
  • 1
    What do you call type, to the extension? Commented Aug 12, 2018 at 14:35
  • Please, provide example of input and output. Do you need pathlib.PurePath.suffix method? Commented Aug 12, 2018 at 14:58

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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') 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.