我的代码给我一个类型错误,即使我已经照顾过类型...请告诉我我哪里出错了

问题描述 投票:-1回答:1

我写了下面的代码,将具有特定格式的日期的文件转换为另一种格式我得到这个错误 在第13行的“ E:\ pranil \ python \使用python \ american日期将内容添加到European.py”中 os.rename(文件名,替换者)TypeError:重命名:src应该是字符串,字节或os.PathLike,而不是list

import os, random, re
os.chdir('.\\american date files')
for i in range(20):
    file = open(f'{random.randint(1, 12)}-{random.randint(1, 31)}-{random.randint(2000, 2099)}', 'w')
    file.close()
ame_dates = os.listdir()
ame_date = re.compile(r'(\d\d?)-(\d\d?)-(\d\d\d\d)')
euro_dates = [ame_date.sub(f'{ame_date.search(date).group(2)}-{ame_date.search(date).group(1)}-{ame_date.search(date).group(3)}', date) for date in ame_dates]
count = 0
for dirname, foldername, filename in os.walk(r'E:\pranil\python\doing stuff with python\american date files'):
    replacer = euro_dates[count]
    os.rename(filename, replacer)
    count += 1
python-3.x regex for-loop operating-system
1个回答
0
投票

os.walk返回字符串和数组的元组,因此您的filename对象看起来像这样:['filename']

© www.soinside.com 2019 - 2024. All rights reserved.