Reading file issue

L

loial

I am parseing a file to extract data, but am seeing the file being updated even though I never explicitly write to the file. It is possible that another process is doing this at some later time, but I just want to check that opening the file as follows and ignoring a record would not result in that record being removed from the file.

I'm damned sure it wouldn't, but just wanted to check with the experts!.


for line in open("/home/john/myfile"):
linecount = linecount + 1

if linecount == 1: # ignore header
continue
 
C

Chris Angelico

I am parseing a file to extract data, but am seeing the file being updated even though I never explicitly write to the file. It is possible that another process is doing this at some later time, but I just want to check that opening the file as follows and ignoring a record would not result in that record being removed from the file.

I'm damned sure it wouldn't, but just wanted to check with the experts!.

for line in open("/home/john/myfile"):

Absolutely not. You're opening the file (by default) for reading only.
That's not going to edit the file in any way. (It might cause the
directory entry to be rewritten, eg last-access time, but not the file
contents.) Your expectation is 100% correct.

ChrisA
 
O

Oscar Benjamin

I am parseing a file to extract data, but am seeing the file being updated even though I never explicitly write to the file. It is possible that another process is doing this at some later time, but I just want to check that opening the file as follows and ignoring a record would not result in that record being removed from the file.

I'm damned sure it wouldn't, but just wanted to check with the experts!.


for line in open("/home/john/myfile"):

The line above opens the file in read-only mode. It's not possible to
make changes to the file if you only open it in read-only mode. So no
this code is not modifying the file. It is, however, slightly better
to write the above as

with open('/home/john/myfile') as fin:
for line in fin:
# stuff

This is better as the "with" statement handles errors better than just
calling open directly.
linecount = linecount + 1

if linecount == 1: # ignore header
continue

Another way of achieving this would be to do:

headerline = fin.readline()
for line in fin:
# No need to worry about that header line now


Oscar
 
T

Tim Chase

I am parseing a file to extract data, but am seeing the file being
updated even though I never explicitly write to the file. It is
possible that another process is doing this at some later time, but
I just want to check that opening the file as follows and ignoring
a record would not result in that record being removed from the
file.

The only complication I'd see would be the reader bombing out because
the writer process is in the middle of writing. A quick test on
WinXP showed that it's possible to continue to write to a file that
another process has open for reading (this shouldn't be an issue on
POSIX OSes; Win32 can be a bit more fascist about sharing files,
especially if they're both open for writing). However, that doesn't
alter the data written, so all it takes is just re-running the reader
process.

-tkc
 

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
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top