I received csv file with sysdate attached to the filename with format yyyy-mm-dd
. For example today i received file as User_test_2023-10-20.csv
.
I have below python code where i want to attach this sysdate with format yyyy-md-dd
to the filename but not sure how to do this.
import codecs
import shutil
with codecs.open(r"C:\User_test_.csv", encoding="utf-16") as input_file:
with codecs.open(r"C:\User_test_.csv", "w", encoding="utf-8") as output_file:
shutil.copyfileobj(input_file, output_file)
Output:
C:/User_test_2023-10-20.cs
I will add though:
Since it seems you want to change the encoding:
Which I'm not sure if it'll work, you whould use different filenames or close one file before overwriting it:
Note: If you didn't need to change encoding you could use
.seek(0)
and.truncate()
to read and write in a single opening of the file.