【发布时间】:2020-04-20 03:18:25
【问题描述】:
所以我正在尝试重命名已下载到我的下载文件夹的文件。
我的代码:
import os
from time import strftime
current_time = strftime("(%x-%Xp)")
old_name = r'C:/Users/name/Downloads/file.pdf'
new_name = r'C:/Users/name/Downloads/file'+current_time+'.pdf'
os.rename(old_name, new_name)
但是,我不断收到此错误:
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:/Users/darianmoore/Downloads/file.pdf' -> 'C:/Users/darianmoore/Downloads/file(12/31/19-10:47:47AM).pdf'
我很困惑,因为如果我使用此代码它可以正常工作,但它不是我想要的格式:
import os
from time import strftime
current_time = strftime("(%m%d%y-%I%M%p)")
old_name = r'C:/Users/name/Downloads/file.pdf'
new_name = r'C:/Users/name/Downloads/file'+current_time+'.pdf'
os.rename(old_name, new_name)
【问题讨论】:
-
你想得到什么格式?
-
我认为您不能在 Windows 中为文件名添加冒号或斜杠。
-
解决方法:只需格式化时间字符串,使其不包含任何斜杠和冒号。