简体   繁体   中英

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

I am trying to determine the file extension using os.path.splitext

here is my code:

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))

here is the error readout from 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 #####

NoneType should mean I am not passing anything, when I print type(filename) i get:

how do I pass the file so I can determine extension and then create a dataframe? do I need to save to disk

this will be for a parsing script that gets regularly scheduled csv'ed emailed to it, hence the regex to determine which rule to follow.

In extension = os.path.splitext(fileName) , you're using the variable fileName , instead of filename . Python is case-sensitive, so these are different variables. Evidently, fileName is elsewhere set to None, and changing that instance to filename should fix this error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM