python如何区分文件类型_python--magic module 文件类型识别

前言:接触magic module是由于工作中对的文件类型判断需求,网上查了下,python内置的有mimetypes module,filetype module,与使用mimetypes库相比,更可靠的方法是使用magic软件包。

magic

magic是libmagic文件标识库的封装,libmagic是一个根据文件头识别文件类型的开发库,因此可以实现对文件类型的判断,在Django上,还可以确保MIME类型与UploadedFile.content_type相匹配。

libmagic

module name: magic

install:

python setup.py install # or

easy_install . # or

pip install file-magic

Usage:

import magic

detected = magic.detect_from_filename('magic.py')

print 'Detected MIME type: {}'.format(detected.mime_type)

print 'Detected encoding: {}'.format(detected.encoding)

print 'Detected file type name: {}'.format(detected.name)

确定文件mime-type时,选择的工具简称为其后端称为libmagic。如果您想将任何libmagic绑定与python一起使用,则需要使用此工具,该工具已经附带了自己的python绑定

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UnpicklingError Traceback (most recent call last) Input In [66], in <cell line: 36>() 30 Kcat_model = model.KcatPrediction(device, n_fingerprint, n_word, 2*dim, layer_gnn, window, layer_cnn, layer_output).to(device) 31 ##‘KcatPrediction’是一个自定义模型类,根据给定的参数初始化一个Kcat预测模型。使用了上述参数,如果要进行调参在此处进行 32 # directory_path = '../../Results/output/all--radius2--ngram3--dim20--layer_gnn3--window11--layer_cnn3--layer_output3--lr1e-3--lr_decay0/archive/data' 33 # file_list = os.listdir(directory_path) 34 # for file_name in file_list: 35 # file_path = os.path.join(directory_path,file_name) ---> 36 Kcat_model.load_state_dict(torch.load('MAEs--all--radius2--ngram3--dim20--layer_gnn3--window11--layer_cnn3--layer_output3--lr1e-3--lr_decay0.5--decay_interval10--weight_decay1e-6--iteration50.txt', map_location=device)) 37 ##表示把预训练的模型参数加载到Kcat_model里,‘torch.load’表示函数用于文件中加载模型参数的状态字典(state_dict),括号内表示预训练参数的文件位置 38 predictor = Predictor(Kcat_model) File ~/anaconda3/lib/python3.9/site-packages/torch/serialization.py:815, in load(f, map_location, pickle_module, weights_only, **pickle_load_args) 813 except RuntimeError as e: 814 raise pickle.UnpicklingError(UNSAFE_MESSAGE + str(e)) from None --> 815 return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args) File ~/anaconda3/lib/python3.9/site-packages/torch/serialization.py:1033, in _legacy_load(f, map_location, pickle_module, **pickle_load_args) 1027 if not hasattr(f, 'readinto') and (3, 8, 0) <= sys.version_info < (3, 8, 2): 1028 raise RuntimeError( 1029 "torch.load does not work with file-like objects that do not implement readinto on Python 3.8.0 and 3.8.1. " 1030 f"Received object of type "{type(f)}". Please update to Python 3.8.2 or newer to restore this " 1031 "functionality.") -> 1033 magic_number = pickle_module.load(f, **pickle_load_args) 1034 if magic_number != MAGIC_NUMBER: 1035 raise RuntimeError("Invalid magic number; corrupt file?") UnpicklingError: invalid load key, 'E'. 这个问题怎么解决
最新发布
07-14

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值