[Tutor] help---- how do i create a file with todays date as
Sat, 02 Dec 2000 23:46:08 -0500
That raises an interesting question, isn't the following redundant?
>>> import time
>>> from time import time, localtime, strftime
By that I mean, by entering 'import time' aren't you essentially doing the
same thing as 'from time import time, localtime, strftime' except that with
the latter you are importing specific classes?
Daniel Yoo wrote:
> On Sat, 2 Dec 2000, JD, Venugopal (CORP, CIM, MMC) wrote:
> > i a programming newbie and would like to know if there is any way i
> > can create or rename a file through python so that the file name
> > corresponds to todays date.
> Sure! There are a few tools that you can use to do this. One of them is
> the renaming function in the "os" module: os.rename(). Here's some sample
> >>> import os
> >>> open("foobar.txt", "w")
> <open file 'foobar.txt', mode 'w' at 0x81c7d68>
> >>> os.rename("foobar.txt", "barfoo.txt")
> >>> # does that file exist?
> ... os.path.isfile("foobar.txt")
> >>> os.path.isfile("barfoo.txt")
> You can read a little more about os functions that deal with files and
> Getting the time isn't too difficult either: the time module handles a lot
> of the dirty work for us. Take a look here:
> For example:
> >>> import time
> >>> from time import time, localtime, strftime
> # time() gives us the number of milliseconds that have passed since the
> # "Epoch"
> # localtime() takes these milliseconds, and returns a tuple of
> # month,year,day,second,... lots of information.
> # strftime() takes this tuple, and a formatting string, and returns the
> # date in that form.
> >>> time_tuple = localtime(time())
> >>> print strftime("%m%y%d", time_tuple) # Month, Year, Day as decimals
> You'll definitely want to look at the module documentation and play around
> with it. Also, if you're doing anything with file renaming, make some
> backups of those files, just in case. *grin*
> Good luck!
> Tutor maillist - Tutor@python.org