how can I name my csv file as timestamp when getting data from twint

350 Views Asked by At

I am using twint for web crawling data form twitter. For that reason whenever I save my output file as csv, I want to name current timestamp but it is giving me an error

inport twint
c = twint.Config()
c.Search = "NationBehindState"
c.Limit = 10
c.Store_csv = True
c.Output =str(dt_string)+"g.csv"

enter image description here

However if I simply name any file with string in c.Output, file is created without any issue

1

There are 1 best solutions below

0
On

Try this:

from datetime import datetime
dt = datetime.now()
date = dt.strftime('%Y_%m_%d_%H_%M_%S')
file = open(f'{date}.csv', 'w+')
file.write(data)
file.close()

This will give you accuracy down the second and includes the year so will never risk repeating. If you need other formatting, look here: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes