OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect

Viewed 19011

I am writing python program to rename the file with current time & date, but I get below error.

OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect

my code

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")

output

OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect

Please let me know what mistake I am making for os.rename function when passing z & destination new strings.

1 Answers
Related