我正在寫python腳本來從dir中取出一個一個的文件,並且如果它的mimetype不是JSON,那麼就得到它mimetype然後我想忽略它。請參閱下面我的腳本我怎麼能從python中沒有文件中過濾掉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文件。 – larsks