Python:AttributeError:'NoneType'对象没有属性'rfind'

rip*_*ick 4 python file-extension

我试图使用os.path.splitext确定文件扩展名

这是我的代码:

for emailid in msgs:
    typ, s = mail.fetch(emailid, "(RFC822)")
    m = email.message_from_string(s[0][1])
    result = re.findall(pattern,str)
    if result:     
        for part in m.walk():
            filename = part.get_filename()
            if filename:
                extension = os.path.splitext(fileName)
                if extension[1][1:] == 'csv':      
                    ### Process into a dataframe
                    df = pd.read_csv(StringIO(filename))
Run Code Online (Sandbox Code Playgroud)

这是来自IPython的错误读数:

#### Begin error Message #####

    AttributeError                            Traceback (most recent call last)
<ipython-input-83-71fabd157c43> in <module>()
     40             if filename:
     41                 print type(filename)
---> 42                 extension = os.path.splitext(fileName)
     43                 if extension[1][1:] == 'csv':
     44                     ### Process into a dataframe

C:\Anaconda2\lib\ntpath.pyc in splitext(p)
    198 
    199 def splitext(p):
--> 200     return genericpath._splitext(p, sep, altsep, extsep)
    201 splitext.__doc__ = genericpath._splitext.__doc__
    202 

C:\Anaconda2\lib\genericpath.pyc in _splitext(p, sep, altsep, extsep)
     97     leading dots.  Returns "(root, ext)"; ext may be empty."""
     98 
---> 99     sepIndex = p.rfind(sep)
    100     if altsep:
    101         altsepIndex = p.rfind(altsep)

AttributeError: 'NoneType' object has no attribute 'rfind'

#### End Error message #####
Run Code Online (Sandbox Code Playgroud)

NoneType应该意味着我没有传递任何东西,当我打印类型(文件名)我得到:

如何传递文件以便我可以确定扩展名然后创建数据框?我需要保存到磁盘吗?

这将是一个解析脚本,它通过电子邮件定期发送给csv'ed,因此正则表达式确定要遵循的规则.

mwc*_*ase 5

extension = os.path.splitext(fileName),你使用变量fileName,而不是filename.Python区分大小写,因此这些是不同的变量.显然,fileName在别处设置为None,并将该实例更改为filename应该修复此错误.