file type in python

没有找到与「file type in python」相关的内容

AI 回答 由 glm-4-flash 生成 · 仅供参考,请自行核实
1. 使用`mimetypes`模块检测文件类型

import mimetypes

file_type = mimetypes.guess_type('path/to/file')
print(file_type)

2. 使用`os`模块读取文件头信息

import os

file_type = os.popen(f'file --mime-type -b {os.path.abspath("path/to/file")}`).read().strip()
print(file_type)

3. 使用`magic`库(需要安装)

from magic import from_buffer

file_type = from_buffer(open('path/to/file', 'rb').read(), mime=True)
print(file_type)
没有找到「file type in python」相关的内容

建议:检查关键词是否有错别字,或换一个更常见的说法。