Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Reading file issue (http://www.velocityreviews.com/forums/t957004-reading-file-issue.html)

loial 01-28-2013 11:47 AM

Reading file issue
 
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



Chris Angelico 01-28-2013 11:57 AM

Re: Reading file issue
 
On Mon, Jan 28, 2013 at 10:47 PM, loial <jldunn2000@gmail.com> wrote:
> 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

Oscar Benjamin 01-28-2013 11:58 AM

Re: Reading file issue
 
On 28 January 2013 11:47, loial <jldunn2000@gmail.com> wrote:
> 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

loial 01-28-2013 12:03 PM

Re: Reading file issue
 
Thanks for confirming my sanity



On Monday, 28 January 2013 11:57:43 UTC, Chris Angelico wrote:
> On Mon, Jan 28, 2013 at 10:47 PM, loial <jldunn2000@gmail.com> wrote: > Iam 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



loial 01-28-2013 12:03 PM

Re: Reading file issue
 
Thanks for confirming my sanity



On Monday, 28 January 2013 11:57:43 UTC, Chris Angelico wrote:
> On Mon, Jan 28, 2013 at 10:47 PM, loial <jldunn2000@gmail.com> wrote: > Iam 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



Tim Chase 01-28-2013 12:14 PM

Re: Reading file issue
 
On Mon, 28 Jan 2013 03:47:07 -0800 (PST) loial <jldunn2000@gmail.com>
wrote:

> 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




All times are GMT. The time now is 09:50 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.