我是 Python 新手,在保存重命名文件列表时遇到问题,因此我最终可以将文件移动到新目录。我的代码贴在下面:
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)
基本上,这段代码的作用是遍历原始文件名(orig),并将它们重命名为特定的约定(new_name)。我遇到的问题是每次迭代时,我都试图将每个 new_nam 保存到列表“new_files”中,但是我不断收到此错误:
line 83, in <module>
new_files = new_files.append(new_nam)
AttributeError: 'NoneType' object has no attribute 'append'
基本上我认为它是说你不能将一个“无类型”添加到一个有意义的列表中,但是当我打印所有的 new_nam 时,它们都是不是 None 类型的字符串。所以我想我不知道为什么这段代码没有将每个新文件名添加到 new_files 列表中。非常感谢任何提示建议,无法弄清楚这一点:/谢谢!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句