Reading file issue

Discussion in 'Python' started by loial, Jan 28, 2013.

  1. loial

    loial Guest

    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
    loial, Jan 28, 2013
    #1
    1. Advertising

  2. On Mon, Jan 28, 2013 at 10:47 PM, loial <> 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
    Chris Angelico, Jan 28, 2013
    #2
    1. Advertising

  3. On 28 January 2013 11:47, loial <> 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
    Oscar Benjamin, Jan 28, 2013
    #3
  4. loial

    loial Guest

    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 <> 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, Jan 28, 2013
    #4
  5. loial

    loial Guest

    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 <> 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, Jan 28, 2013
    #5
  6. loial

    Tim Chase Guest

    On Mon, 28 Jan 2013 03:47:07 -0800 (PST) loial <>
    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
    Tim Chase, Jan 28, 2013
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darrel
    Replies:
    3
    Views:
    660
    Kevin Spencer
    Nov 11, 2004
  2. Shane
    Replies:
    7
    Views:
    358
    Shane
    Aug 23, 2004
  3. John

    reading text file issue

    John, Jun 2, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    340
    Cowboy \(Gregory A. Beamer\)
    Jun 2, 2006
  4. Replies:
    0
    Views:
    773
  5. Karim Ali

    Reading a file and resuming reading.

    Karim Ali, May 25, 2007, in forum: Python
    Replies:
    2
    Views:
    359
    Hrvoje Niksic
    May 25, 2007
Loading...

Share This Page