PDA

View Full Version : Need some help with files


kiwipenguin
10-28-2002, 06:25 AM
OK, I'm a complete beginner when it comes to most forms of programming. I have written a Python script that reads my backup logs for me, and creates a new log file containing only the data I need to know. It's turned huge logs files into 6 line files, much quicker to read.

Now, my problem is I want to create a new text file every day, with the date as the file name. Below is some horribly failed code. Any help would be appreciated.


import time

blah = time.asctime()
print blah

file = open("blah.txt","w")
file.close

Benny
10-28-2002, 06:51 AM
Just do something like this:


import time
filename = time.strftime("%d-%m-%y.txt", time.gmtime())

file = open(filename, "w")


That would open a file for writing called:

dd-mm-yy.txt

Where dd is the current day ( eg 28 ), mm is the current month ( eg 10 ) and yy is the current year ( eg 02 ).

If this format doesn't suit then read up on the time module at python.org - http://www.python.org/doc/current/lib/module-time.html

Hope this gives you enough to go on with.

Enjoy Python.

Cheers
Ben

Strike
10-28-2002, 08:54 AM
Note: use the file command instead of the open command for 2.2 and later. This, of course, means that your variable will have to be named something other than file

GnuVince
10-28-2002, 09:01 AM
Strike: file? What's new about it?

kmj
10-28-2002, 09:40 AM
Gnuvince: they replaced "open" with "file" because they made it so you can inherit from all the builting types (file, str, int, list, dict, etc) .. for all basic types, you use the name of the type to create an object of the type. Functionally 'open' and 'file' are the same; 'open' is just deprecated.

jemfinch
10-28-2002, 03:06 PM
Functionally they're close; you can subclass "file", you can't subclass "open".

Jeremy