How to check type of files without extensions with Python?

Sometimes, we want to check type of files without extensions with Python.

In this article, we’ll look at how to check type of files without extensions with Python.

How to check type of files without extensions with Python?

To check type of files without extensions with Python, we can use the magic library.

To install it, we run

pip install python-magic

Then we write

import magic

s = magic.from_file('iceland.jpg')

to call magic.from_file to read iceland.jpg and get the file’s actual MIME type as a string.

Conclusion

To check type of files without extensions with Python, we can use the magic library.