Is there a way or a package to guess the type of a file in Python? For example, is it a way to detect a file could be open as ascii, unicode or binary?
Thanks in advance!
Is there a way or a package to guess the type of a file in Python? For example, is it a way to detect a file could be open as ascii, unicode or binary?
Thanks in advance!
If you're on a Unix OS (Linux or Mac), you have access to magic
. If on a Mac, you'll likely need to brew install libmagic
. There's a Python library called filemagic for rolling it into your Python scripts.
import magic
mage = magic.Magic()
mage.id_buffer("adsfadsf←")
The last line will return 'UTF-8 Unicode text, with no line terminators'
You can also have it check files, which isn't based on the filename but rather the magic bytes at the start of the file: