Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Auto-Name Each Subsequent .CSV File of Scraped Date?

I'd like to run this code daily and write it automatically to a .csv file. I'm sure whether I'd prefer to have each day be its own file or have each day amend an existing file. If I go with the former, which is what I think I want, I'd like to have each file be auto-named with the date the code was run. Presently I know how to write to a single file that gets over-written each time I run the code. I've done it like this:

str1 = "\n".join(data)
outFile = open('sampledata.csv', 'write')
outFile.write(str1)
outFile.close()

How do I code this to automatically save to a new .csv file each time and name each subsequent file with the date the code was run?

like image 994
PythonFisher Avatar asked Jan 27 '26 03:01

PythonFisher


2 Answers

Simply use the datetime module's datetime.now() function to get the current date and time. If you want a specific format use the strftime function to pick the exact format you want. Otherwise, pick a sane default by just using datetime.datetime.now().date().isoformat().

>>> import datetime
>>> now = datetime.datetime.now()
>>> now.strftime("%Y-%m-%d")
'2013-11-01'
>>> datetime.datetime.now().date().isoformat()
'2013-11-01'

Bringing your code in, the final is this:

import datetime
str1 = "\n".join(data)

# Get the current date and time
now = datetime.datetime.now()
now_str = now.strftime("%Y-%m-%d")

# Write out to a file for today
outfilename = 'sampledata-{}.csv'.format(now_str)

outFile = open(outfilename, 'write')
outFile.write(str1)
outFile.close()

If you want hours, minutes, seconds, and microseconds, don't make the call to date (it truncates the timestamp):

>>> import datetime
>>> datetime.datetime.now().isoformat()
'2013-11-01T21:55:45.465662'

However, if you're running this on Windows it won't accept the ':' in the filename. Linux and Macs are ready to go though.

like image 74
Kyle Kelley Avatar answered Jan 29 '26 17:01

Kyle Kelley


The best way would be to have it run a loop or store a state variable so each subsequent run is incremented, i.e.

str1 = "\n".join(data)
outFile = open('sampledata'+str(i)+'.csv', 'write')
outFile.write(str1)
outFile.close()

In my code, 'i' is just an integer value being kept track of somewhere in the code.

like image 34
Dylan Lawrence Avatar answered Jan 29 '26 17:01

Dylan Lawrence