使用Python读取MIME类型时出错

时间:2015-05-21 12:52:45

标签: python-2.7 mime-types

我正在编写一个python脚本,它将读取文件扩展名,MIME类型和文件签名,以便我可以确定其中是否有任何缺失或损坏,并确定给定目录中文件的类型。

到目前为止,我已经:

import magic, os

def get_ext(dirPath):
    foldercount = 0
    filecount = 0
    while True:
        if os.path.exists(dirPath):
            break
        else:
            print "Directory doesn't exist!"
            continue
    includePath = raw_input("Do you want to include the complete path to the files in the output?: Y/N\n")

    if includePath.upper() == "Y":
        for rootfolder, subfolders, files in os.walk(dirPath):
            foldercount += len(subfolders)
            filecount += len(files)
            for f in files:
                name = f
                path = os.path.join(rootfolder, f)
                ext = os.path.splitext(f)[1]
                if ext != "":
                    print "Filename: " + str(path) + "\t\tExtension: " + str(ext) + "\tMIME: "
                else:
                    print "Filename: " + str(path) + "\t\tExtension: no extension found"
        print "Found {0} files in {1} folders".format(filecount, foldercount)

    elif includePath.upper() == "N":
        for rootfolder, subfolders, files in os.walk(dirPath):
            foldercount += len(subfolders)
            for f in files:
                name = f
                path = os.path.join(rootfolder, f)
                ext = os.path.splitext(f)[1]
                if ext != "":
                    print "Filename: " + str(name) + "\t\tExtension: " + str(ext)
                else:
                    print "Filename: " + str(name) + "\t\tExtension: no extension found"
        print "Found in {0} folders".format(foldercount) 

    else:
        print "Wrong input, try again"


def getMagic(dirPath):
    while True:
        if os.path.exists(dirPath):
            break
        else:
            print "Directory doesn't exist!"
            continue
    for rootfolder, subfolders, files in os.walk(dirPath):
        for f in files:
            bestand = f 
            mymagic = magic.Magic(mime=True)
            mytype = mymagic.from_file(bestand)
            print mytype
            print ("The MIME type of the file %s is %s" %(bestand, mytype))

dirPath = raw_input("Directory to check files in: ")        
get_ext(dirPath)       
getMagic(dirPath)   

get_ext()正常工作,给我一个文件名和扩展名。 但是,当我尝试获取MIME类型时,它会以某种方式抛出以下错误:

Traceback (most recent call last):
  File "/home/nick/workspace/Proto/asdfasdf.py", line 80, in <module>
    getMagic(dirPath)     
  File "/home/nick/workspace/Proto/asdfasdf.py", line 74, in getMagic
    mytype = mymagic.from_file(bestand)
  File "/usr/local/lib/python2.7/dist-packages/magic.py", line 75, in     from_file
    raise IOError("File does not exist: " + filename)
IOError: File does not exist: 2

我知道文件&#39; 2&#39;确实存在,是一个纯文本文档。 如果我在脚本中硬编码文件的路径,它确实给了我MIME,但我希望脚本遍历一个目录,给我所有mimes文件。

有人可以解释为什么会抛出此错误以及如何解决此问题? 我使用的是使用pip install python-magic

安装的python-magic模块

由于

1 个答案:

答案 0 :(得分:0)

os.walk的文档中我们可以看到

  

filenames是dirpath中非目录文件的名称列表。请注意,列表中的名称不包含路径组件。要获取dirpath中文件或目录的完整路径(以top开头),请执行os.path.join(dirpath,name)。

您需要获得完整的路径

bestand = os.path.join(rootfolder, f)
相关问题