我正在编写python程序,以使用当前时间和日期来重命名文件,但出现以下错误。
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect
Run Code Online (Sandbox Code Playgroud)
我的代码
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")
Run Code Online (Sandbox Code Playgroud)
输出
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect
Run Code Online (Sandbox Code Playgroud)
请让我知道os.rename在传递z和目标新字符串时我在函数中犯什么错误。