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!