为什么strftime(“%x”)在我的python代码中不起作用? [重复]

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

因此,我正在尝试重命名已下载到我的下载文件夹中的文件。

我的代码:

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)
python strftime
1个回答
4
投票

文件名不能包含斜杠。 file(12/31/19-10:47:47AM).pdf可以。

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