我发现了很多问题,这些问题帮助我达到了现在的水平,但我一直在前进。
最终目标:我有大约 1500 个带有无意义名称的音频文件(每个 9 个字符长,数字和字母的混合;即 4a96det8g)。我有一个表格,其中一列名称无意义,第二列名称有用(例如:Getting There)。我想将所有文件重命名为与无意义名称一致的有用名称,然后保留每个文件上的任何扩展名(.m4a 和 .mp4 的混合)。
我在 Python 中所处的位置:我已将表拆分为 2 个不同的 CSV 文件,并将它们作为列表加载到 IDLE 中:ID_List(无意义的名称)和 Names_List(有用的列表)。我没有在 Python 中对列表进行排序,因此每个索引都适用于另一个列表中的相同索引。
这是我到目前为止的代码:
for i in ID_List:
for file in os.listdir("/Volumes/2TB Portabl/Audios copy 2"):
if(file.startswith(ID_List[i])):
oldtext = os.path.splitext(file)[1]
os.rename(file, Names_List[i] + oldtext)
我得到的错误是:
Traceback (most recent call last):
File "<pyshell#30>", line 3, in <module>
if(file.startswith(ID_List[i])):
TypeError: list indices must be integers or slices, not str
所以在某处我需要强制从 ID_List 返回一个字符串???或者也许有更简单的方法可以一起完成这一切?
您遍历列表中的值,而不是索引。这就是为什么TypeError: list indices must be integers or slices, not str
会发生。尝试按索引迭代。
for i in range(len(ID_List)):
for file in os.listdir("/Volumes/2TB Portabl/Audios copy 2"):
if(file.startswith(ID_List[i])):
oldtext = os.path.splitext(file)[1]
os.rename(file, Names_List[i] + oldtext)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句