Python 重命名错误"file not found"错误



我的代码有问题。我是Python的新手,但我正在尝试制作一个脚本来重命名文件夹(+子文件夹(中超过3年的所有文件。它适用于文件夹中的文件,但当涉及到子文件夹中的文件夹时,我会收到"找不到文件"的错误。

这是我的代码:

import sys, os.path, time, datetime
count = 0
for (dirname, dirs, files) in os.walk('.'):
for filename in files:
thefile = os.path.join(dirname,filename)    
today = datetime.datetime.today()
modified_date = datetime.datetime.fromtimestamp(os.path.getmtime(thefile))
duration = today - modified_date
if duration.days > 1095:
old = filename
new = 'old_' + old
print(thefile, "Last modified: %s" % time.ctime(os.path.getmtime(thefile)), os.path.getmtime(thefile))
count = count + 1
os.renames(old, new)
print("number of old files: ", count)

有人能帮我吗?

您需要提供文件的完整路径

更新

old = filename
new = 'old_' + old

old = os.path.join(dirname, filename)
new = os.path.join(dirname, 'old_' + filename)

相关内容

最新更新