file write question

  • Thread starter Robb Lane (SL name)
  • Start date
R

Robb Lane (SL name)

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
 
H

Hrvoje Niksic

Robb Lane (SL name) said:
... 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.
 
T

thebjorn

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
 
R

Robb Lane (SL name)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top