introduce
Magic is a python module to identify the file type
the original project address: https://github.com/ahupp/python-magic
the project integration the Windows needed libraries, directly use this one instead:
https://github.com/julian-r/python-magic
The installation
pip install python-magic-bin
Simple to use
Method 1
>>> import magic
>>> magic.from_file("testdata/test.pdf")
'PDF document, version 1.2'
>>> magic.from_buffer(open("testdata/test.pdf").read(1024))
'PDF document, version 1.2'
>>> magic.from_file("testdata/test.pdf", mime=True)
'application/pdf'
Method 2
>>> f = magic.Magic(uncompress=True)
>>> f.from_file('testdata/test.gz')
'ASCII text (gzip compressed data, was "test", last modified: Sat Jun 28 21:32:52 2008, from Unix)'
A small example of solving a real problem
This example combines the use of Zipfile and Magic
# coding:utf8
import os
from zipfile import ZipFile
import magic
m = magic.Magic()
dir_name = '/home/some_zip/'
for root, dir, files in os.walk(dir_name):
zipnames = files
break
for a_zip_name in zipnames:
print '#' * 80
print a_zip_name
with ZipFile(dir_name + a_zip_name) as azip:
filenames = azip.namelist()
for i in [10, 20, 30]:
with azip.open(filenames[i], pwd='zippassword') as afile:
print m.from_buffer(afile.read())
Read More:
- R load(xxx.Rdata) Error bad restore file magic number (file may be corrupted) — no data loaded
- The method of constructing even order magic square (n = 4 * m)
- Tensorflow operation report error modulenotfounderror: no module named ‘tensorflow. Python. Types’ solution
- Eclipse .java File Syntax error, parameterized types are only available if source level is 1.5 or
- Idea | identify non self built webapp folder
- Unsupported operation types unsupported operation data types
- When Python Django creates a database, can’t open file ‘ manage.py ‘: [Errno 2] No such file or directory
- Python read / write file error valueerror: I/O operation on closed file
- Python how does a. Py file call classes and functions in another. Py file
- python reads csv file is an error _csv.Error: iterator should return strings, not bytes (did you open the file in text)
- About java “Error: bad binary operator types”
- Can’t find Python executable “D:\python3\python.exe”, you can set the PYTHON env variable.
- Typeerror: UFUNC ‘isn’t supported for the input types
- C++ —Return multiple values of different types
- No code, a solution to the error in the path of reading CSV file by Python: filenotfounderror: [errno 2] no such file or directory: ‘XX. CSV‘
- Error c2371: ‘xxx’: redefinition; different basic types solutions
- Cannot find module ‘internal / util / types’ appears during gulp build
- Python reports oserror: initializing from file failed
- fatal error: Python.h: No such file or directory
- Error when Python rarfile decompresses. Rar file: badrarfile