本文介绍了如何从文件类型获取文件扩展名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下文件名列表

files = [
    '/dl/files/4j55eeer_wq3wxxpiqm.jpg',
    '/home/Desktop/hjsd03wnsbdr9rk3k',
    'kd0dje7cmidj0xks03nd8nd8a3',
    ...
]

问题是大多数文件的文件名都没有扩展名,什么是获取这些文件扩展名的最佳方法是什么?

The problem is most of the files do not have an extension in the filenames, what would be the best way to get file extension of these files ?

我什至不知道这是否可行,因为python会将所有文件视为没有任何filetype关联的bufferstring对象.

I don't know if this is even possible because python would treat all files as buffer or string objects that do not have any filetype associated with them.

这完全可以做到吗?

推荐答案

一旦使用magic获取MIME类型,就可以使用 mimetypes.guess_extension() 以获得扩展名.

Once you use magic to get the MIME type, you can use mimetypes.guess_extension() to get the extension for it.

这篇关于如何从文件类型获取文件扩展名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 03:30
查看更多