开发者

identifying the format of files

开发者 https://www.devze.com 2023-04-12 22:51 出处:网络
In linux, we have a utility called “file”, which helps us to determine the identification of a file. Is there any pyth开发者_如何学Con module that can do the same job?

In linux, we have a utility called “file”, which helps us to determine the identification of a file. Is there any pyth开发者_如何学Con module that can do the same job?

I don't prefer to use subprocess.Popen(['file', 'blah.blah']), because it is platform dependent. For instance, windows do not have “file” (although it can be downloaded).


magic

Don't forget the DLL.


Ignacio mentioned the Magic library, but you could also do it with the standard library if you're confident that the filename is enough to know the file type from (by extension):

>>> import mimetypes
>>> mimetypes.guess_type('__init__.py')
('text/x-python', None)

See the Mimetypes module in the standard library. This however isn't a replacement for actually inspecting the content of the file and figuring out its type.


There are standard modules imghdr and sndhdr for graphic and sound files, respectively.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号