This question already has an answer here:
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!
This question already has an answer here:
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 tobrew install libmagic
. There's a Python library called filemagic for rolling it into your Python scripts.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:
You want the filemagic module.