【发布时间】:2017-08-10 17:45:36
【问题描述】:
我正在编写 python 程序以用当前时间和日期重命名文件,但出现以下错误。
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect
我的代码
import os
import sys
import datetime
file=open("C:\\Users\\sun\\Desktop\\ping",'w')
z=file.name
dt = str(datetime.datetime.now())
file.close()
print(z)
new ='C:\\Users\\sun\\Desktop\\ping_'+dt+'.txt'
os.rename(z,new)
print("i am done")
输出
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect
请告诉我在传递 z 和目标新字符串时我对 os.rename 函数犯了什么错误。
【问题讨论】:
-
日期可能包含斜杠和冒号...非法文件名。
标签: python python-3.x datetime