[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

file write question

rdrink

1/27/2008 3:03:00 AM

I have written a script which:
- opens a file
- does what it needs to do, periodically writing to the file... for a
few hours
- then closes the file when it's done
So my question is:
Would it be better to 'open' and 'close' my file on each write cycle?
e.g.
def writeStuff(content):
myFile = open('aFile.txt', 'a+')
myFile.write(content)
myFile.close()

.... or just leave it till it's done?
I don't need to use the file contents until the script is done
(although it would be nice... just to check it while it's working), so
just curious what people think is the better method.
- rd
3 Answers

Hrvoje Niksic

1/27/2008 3:25:00 AM

0

"Robb Lane (SL name)" <rdrink@gmail.com> writes:

> ... or just leave it till it's done?
> I don't need to use the file contents until the script is done
> (although it would be nice... just to check it while it's working),

Use flush() to force the contents out at opportune times without
closing and reopening the file object.

thebjorn

1/27/2008 8:54:00 AM

0

On Jan 27, 4:02 am, "Robb Lane (SL name)" <rdr...@gmail.com> wrote:
> I have written a script which:
> - opens a file
> - does what it needs to do, periodically writing to the file... for a
> few hours
> - then closes the file when it's done
> So my question is:
> Would it be better to 'open' and 'close' my file on each write cycle?
> e.g.
> def writeStuff(content):
> myFile = open('aFile.txt', 'a+')
> myFile.write(content)
> myFile.close()
>
> ... or just leave it till it's done?
> I don't need to use the file contents until the script is done
> (although it would be nice... just to check it while it's working), so
> just curious what people think is the better method.
> - rd

Sounds like a classic case for a database to me (long running process
producing sporadic output that you might want to tinker with as it's
being produced). Python 2.5 comes with sqlite3 included...

-- bjorn

rdrink

1/28/2008 11:59:00 PM

0

H -
I am not familiar with flush(), will look into it.
But an interesting note: I repeatedly and often start long running
processes (one running right now: on about it's 14th hour), writing to
open files, with few problems (on Mac OS X). Although of course I
can't look at the results until the file closes...just have to hope
it's right! LOL

B -
You are right about Sqlite, I'm a big fan (moved over from MySQL a few
years ago). But the structure of the data for this project is much
better suited to a 'flat file' format.

Again, not that I am having problems, just thought I'd raise the
topic.

BTW PyCON is in Chicago this year (where I am), maybe I'll meet some
of you there?

RL