Skip to content
Advertisement

Saving a csv with current date and time

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

name_1224_01152021.csv

Can you tell me how to print/save this information, please?

Advertisement

Answer

Here is the code according to your question :

from datetime import datetime

filename = datetime.now().strftime('filename_%H%M_%m%d%Y.csv')

with open(filename, "w+") as f_output:
        csv_output = csv.writer(f_output)
        csv_output.writerow(["row1", "row2"])

in filename you have to write your file name the output of this will be shown as

filename_0620_01152021_.csv 
Advertisement