How to guess the type of a file in python? [duplic

2019-09-16 02:48发布

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!

2条回答
趁早两清
2楼-- · 2019-09-16 03:10

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:

mage check

查看更多
Ridiculous、
3楼-- · 2019-09-16 03:13
登录 后发表回答