将元素添加到 Python 列表时出现“AttributeError”

Bkal05

我是 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 列表中。非常感谢任何提示建议,无法弄清楚这一点:/谢谢!

cs95

list.append是一个就地操作。您必须在不分配返回值的情况下调用该函数:

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

In [123]: data.append(12345)

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

在你的情况下,你需要

new_files.append(new_nam)

文档list.___描述的所有方法都已就位。list

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python-将列表中的字符串元素添加到数组中

来自分类Dev

将元素添加到子列表

来自分类Dev

将列表元素的乘积添加到Python中的现有列表

来自分类Dev

在Python中将多个元素添加到列表

来自分类Dev

如何使用list.insert将Python中的元素添加到列表的末尾?

来自分类Dev

高效地将元素添加到python中的列表

来自分类Dev

将for循环元素添加到列表

来自分类Dev

将新元素添加到列表

来自分类Dev

如何在python中将元素添加到列表中?

来自分类Dev

将python字典添加到列表

来自分类Dev

Python列表理解:将唯一元素添加到列表中?

来自分类Dev

将元素添加到列表与在python中设置相比的时间复杂度

来自分类Dev

将元素添加到列表与在python中设置相比的时间复杂度

来自分类Dev

如何基于Python中的上一个元素将新元素添加到列表中?

来自分类Dev

将列表元素添加到csv

来自分类Dev

将元素添加到链接列表python

来自分类Dev

将字符串添加到列表中的每个元素-Python

来自分类Dev

将元素添加到列表Java

来自分类Dev

将元素均匀添加到列表

来自分类Dev

Python-将列表中的字符串元素添加到数组中

来自分类Dev

将列表元素的乘积添加到Python中的现有列表

来自分类Dev

python将列表的复制元素添加到列表

来自分类Dev

Java将元素添加到列表

来自分类Dev

Python-如何使用计数器将列元素添加到列表

来自分类Dev

将元素添加到键值对列表

来自分类Dev

将python字典添加到列表

来自分类Dev

列表元素不会添加到新列表(Python)

来自分类Dev

以正确的顺序将元素添加到python中的列表

来自分类Dev

将数据添加到列表元素

Related 相关文章

热门标签

归档