Writing Python File at Specific Interval

Denis McMahon denismfmcmahon at gmail.com
Thu Jul 10 01:51:01 CEST 2014


On Wed, 09 Jul 2014 07:36:49 -0700, subhabangalore wrote:

> The code (a basic crawler) would run every morning or evening, on a
> predefined time. [This part is fine].
> 
> In the next part, I am trying to store the daily results to a new file.

So what you want to do is store each day's results in a new file, so 
probably you want to create a filename that looks something like an iso 
8601 date.

Luckily for you python has this functionality available:

https://docs.python.org/2/library/datetime.html#date-objects

$ python
Python 2.7.3 (default, Feb 27 2014, 19:58:35) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import date
>>> fn = date.today().isoformat() + ".log"
>>> print fn
2014-07-10.log
>>> quit()
$

Once you have a string containing your filename, you might use:

fp = open( fn, "w" )
fp.write( data )
fp.close()

-- 
Denis McMahon, denismfmcmahon at gmail.com



More information about the Python-list mailing list