0

I am newer to python and I am having trouble saving a list of renamed files so I can eventually move the files to a new directory. My code is posted below:

import os
new_files = []

        for orig_name in orig:           #This loop splits each file name into a list of stings containing each word
            if '.' in orig_name:            #This makes sure folder names are not changed, only file names
                base = os.path.splitext(orig_name)[0]
                ext = os.path.splitext(orig_name)[1]
                sep = base.split()             #Separation is done by a space
                for t in sep:           #Loops across each list of strings into an if statement that saves each part to a specific variable
                    if t.isalpha() and len(t) == 3:
                        wc = t
                        wc = wc.upper()
                    elif len(t) > 3 and len(t) < 6:
                        wc = t
                        wc = wc.upper()
                    elif len(t) >= 4:
                        pnum = t
                        if pnum.isalnum:
                            pnum = pnum.upper()
                    elif t.isdecimal() and len(t) < 4:
                        opn = t
                        if len(opn) == 2:
                            opn = '0' + opn
                    else:
                        pass
                new_nam = '%s OP %s %s' % (pnum, opn, wc)          #This is the variable that contain the text for the new name
                new_nam = new_nam + ext
                new_files = new_files.append(new_nam)
                print(new_files)

Basically what this code does is loops over the original file names (orig), and renames them to a specific convention (new_name). The issue I am having is for every iteration, I am trying to save each new_nam to the list "new_files" however I keep getting this error:

line 83, in <module>
    new_files = new_files.append(new_nam)
AttributeError: 'NoneType' object has no attribute 'append'

Basically I think it is saying you cannot add a "None Type" to a list which makes sense, however when I print all of the new_nam's, they are all string which is not a None type. So I guess I don't know why this code isn't adding each new file name to the new_files list. Any advice of tips is greatly appreciated, cannot figure this one out :/ Thanks!

1 Answer 1

2

list.append is an inplace operation. You must call the function without assigning the return value:

In [122]: data = [1, 2, 3]

In [123]: data.append(12345)

In [124]: data
Out[124]: [1, 2, 3, 12345]

In your case, you'll need

new_files.append(new_nam)

All the list.___ methods as described in the list docs are inplace.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh wow I knew it was something silly, well thank you for that, learning something new every day!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.