【发布时间】:2016-04-16 12:57:19
【问题描述】:
我正在编写 python 脚本以从 dir 中获取一个一个文件并获取它的 mimetype,如果它的 mimetype 不是 JSON,那么我想忽略它。请参阅我的脚本的以下部分
for filepath in files:
filename = os.path.basename(filepath)
mimetype = mimetypes.guess_type(filepath, strict=False) //here i want to filter out only JSON file and ignore other one
version = "0"
checksum = "0"
fileext = os.path.splitext(filename)[1].lower()
# get raw file data
with open(filepath, "rb") as fr:
filedata = fr.read()
oldfilesize = len(filedata)
在上面的代码中查看我的评论..任何解决方案???
【问题讨论】:
-
您无法真正“获取文件的 mimetype”,因为这不是系统维护的元数据。您可以尝试通过文件扩展名来识别 JSON 文档(查找
.json),但是通过检查很难识别 JSON 文件。
标签: python json mime-types