2017-07-12 3 views
0

j'ai un problème, je dois identifier le type de fichier (tar, tar.gz ou zip) je l'ai trouvé une solution dans ce site: Python - mechanism to identify compressed file type and uncompressDétect python si le fichier est goudron ou tar.gz ou zip

mais la solution fonctionne pas pour le fichier tar, parce que le fichier tar pas même début ... CARACTERE

magic_dict = { 
    "\x1f\x8b\x08": "gz", 
    "\x00\x00\x00": "tar", 
    "\x50\x4b\x03\x04": "zip" 
    } 

max_len = max(len(x) for x in magic_dict) 

def file_type(filename): 
    with open(filename) as f: 
     file_start = f.read(max_len) 
    for magic, filetype in magic_dict.items(): 
     if file_start.startswith(magic): 
      return filetype 
    return "no match" 

Comment puis-je faire détecter fichier tar?

+0

Est-ce python 2 ou python 3? –

+0

j'utilise python 2.7 – user7454761

Répondre