I am running some code and I would like to save a csv file which include the current date and time in its name.
For example: I run some code now (12:24, Jan 15
) and I would like to have something like
JavaScript
x
2
1
name_1224_01152021.csv
2
Can you tell me how to print/save this information, please?
Advertisement
Answer
Here is the code according to your question :
JavaScript
1
8
1
from datetime import datetime
2
3
filename = datetime.now().strftime('filename_%H%M_%m%d%Y.csv')
4
5
with open(filename, "w+") as f_output:
6
csv_output = csv.writer(f_output)
7
csv_output.writerow(["row1", "row2"])
8
in filename you have to write your file name the output of this will be shown as
JavaScript
1
2
1
filename_0620_01152021_.csv
2