[newbie] Read file, and append?

Discussion in 'Python' started by Gilles Ganault, Dec 25, 2007.

  1. Hello

    I'd just like to open each file in a directory with a given extension,
    read it to search for a pattern, and, if not found, append data to
    this file. The following doesn't work:

    ======
    import glob,re

    f = open("activate.tmpl", "r")
    template = f.read()
    template = "\r\n" + template
    f.close()

    for file in glob.glob('*.frm'):
    #BAD f = open(file, "r+")
    #f = open(file, "r")
    #f = open(file, "a")

    f = open(file, "rw")
    if not re.search('Form_Activate',f.read(), re.I):
    #print "Not in " + file

    #IOError: [Errno 0] Error
    f.write(template)

    f.close()
    ======

    What am I doing wrong?

    Thank you.
     
    Gilles Ganault, Dec 25, 2007
    #1
    1. Advertising

  2. Gilles Ganault

    Gary Herron Guest

    Gilles Ganault wrote:
    > Hello
    >
    > I'd just like to open each file in a directory with a given extension,
    > read it to search for a pattern, and, if not found, append data to
    > this file. The following doesn't work:
    >
    > ======
    > import glob,re
    >
    > f = open("activate.tmpl", "r")
    > template = f.read()
    > template = "\r\n" + template
    > f.close()
    >
    > for file in glob.glob('*.frm'):
    > #BAD f = open(file, "r+")
    > #f = open(file, "r")
    > #f = open(file, "a")
    >
    > f = open(file, "rw")
    > if not re.search('Form_Activate',f.read(), re.I):
    > #print "Not in " + file
    >
    > #IOError: [Errno 0] Error
    > f.write(template)
    >
    > f.close()
    > ======
    >
    > What am I doing wrong?
    >

    I've never used "rw" mode, and suspect that's the cause of your
    problems here. I see no need for it here. Since you are going
    to read the whole file in at once, you might as well write it (plus
    the modification) all out again.

    Also I notice that your close is called only if the pattern is not
    found, even though you should call it for *every* file opened.

    Also, you are not using the regular expression machinery as intended.
    You are really just searching for a know and constant substring. Use
    "in" instead.

    Also I *always* read in binary mode (thereby forcing Windows to keep
    it's damn hands off my bytes).

    Also, don't use variables named 'file'. That overrides an important
    builtin.

    Here's what I'd end up with. (This is untested.)


    import glob

    f = open("activate.tmpl", "rb")
    template = "\r\n" + f.read()
    f.close()

    for fname in glob.glob('*.frm'):
    inf = open(fname, "rb")
    content = inf.read()
    inf.close()

    if 'Form_Activate' not in content:
    print "Not in", fname

    outf = open(fname, 'wb')
    outf.write(content)
    outf.write(template)
    outf.close()



    Gary Herron
     
    Gary Herron, Dec 25, 2007
    #2
    1. Advertising

  3. On Tue, 25 Dec 2007 01:14:37 -0800, Gary Herron
    <> wrote:
    >Here's what I'd end up with. (This is untested.)


    Thanks a lot. It worked. I didn't suspect there could be so many
    errors in such a small sript :-D
     
    Gilles Ganault, Dec 25, 2007
    #3
  4. On Tue, 25 Dec 2007 09:44:18 +0100, Gilles Ganault <>
    declaimed the following in comp.lang.python:

    > Hello
    >
    > I'd just like to open each file in a directory with a given extension,
    > read it to search for a pattern, and, if not found, append data to
    > this file. The following doesn't work:


    > f = open(file, "rw")
    > if not re.search('Form_Activate',f.read(), re.I):
    > #print "Not in " + file
    >
    > #IOError: [Errno 0] Error
    > f.write(template)
    >
    > f.close()
    > ======
    >
    > What am I doing wrong?
    >

    Ignoring the question of the proper I/O mode, I believe the I/O
    system MAY require one to perform a seek() when switching from read to
    write and vice versa...
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Dec 25, 2007
    #4
  5. On Tue, 25 Dec 2007 13:27:09 -0800, Dennis Lee Bieber
    <> wrote:
    >Ignoring the question of the proper I/O mode, I believe the I/O
    >system MAY require one to perform a seek() when switching from read to
    >write and vice versa...


    I thought about this, but I don't understand why I would need to do
    this:

    myfile=open('test.txt','r+')
    content = myfile.read()
    myfile.seek()
    myfile.write('added this')
    myfile.close

    Is there really no way to read from a file and append data to it while
    keeping the file open?

    Also, I noticed a problem with the code given by Garry: When opening a
    file in binary mode, the EOF character is 0A, while Windows apps
    expect 0D0A. VisualBasic was not happy :)
     
    Gilles Ganault, Dec 26, 2007
    #5
  6. On Wed, 26 Dec 2007 23:04:33 +0100, Gilles Ganault <>
    declaimed the following in comp.lang.python:


    > myfile=open('test.txt','r+')
    > content = myfile.read()
    > myfile.seek()
    > myfile.write('added this')
    > myfile.close
    >
    >>> tf = open("test.dat", "r+b")
    >>> content = tf.read()
    >>> print content

    Random
    test data
    for an
    open, read, and
    append test
    >>> tf.seek(0, 2) #seek 0-bytes from end of file
    >>> tf.write("\r\nStuff appended")
    >>> tf.close()

    -=-=-=-=-
    Random
    test data
    for an
    open, read, and
    append test
    Stuff appended
    -=-=-=-=-

    Note that, in binary mode, "you" are responsible for every byte in the
    file, including such things as line endings.

    >>> print repr(content)

    'Random\r\ntest data\r\nfor an\r\nopen, read, and\r\nappend test'

    In binary mode, note that this file still has the M$ line endings of
    \r\n AND note that there is NO EOF marker -- the file physically ended
    after the t of test.

    If you open the file in text mode, the \r\n gets translated to a
    simple \n internally, and hence means positioning in the file does match
    the number of bytes in the internal data.

    > Is there really no way to read from a file and append data to it while
    > keeping the file open?
    >


    Works for me, as long as one uses a seek() call when switching
    between read and write...
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Dec 26, 2007
    #6
  7. Gilles Ganault

    John Machin Guest

    Re: Read file, and append?

    On Dec 27, 9:04 am, Gilles Ganault <> wrote:
    > On Tue, 25 Dec 2007 13:27:09 -0800, Dennis Lee Bieber
    >
    > <> wrote:
    > >Ignoring the question of the proper I/O mode, I believe the I/O
    > >system MAY require one to perform a seek() when switching from read to
    > >write and vice versa...

    >
    > I thought about this, but I don't understand why I would need to do
    > this:
    >
    > myfile=open('test.txt','r+')
    > content = myfile.read()
    > myfile.seek()


    The above would not execute; seek needs at least one argument.
    "Perform a seek()" was an abbreviated way of telling you to seek to
    the position at which you want to write i.e. (current) end of file so
    you would use seek(0, 2).

    "Why": already explained; some stdio implementations require it.

    > myfile.write('added this')
    > myfile.close
    >
    > Is there really no way to read from a file and append data to it while
    > keeping the file open?
    >
    > Also, I noticed a problem with the code given by Garry: When opening a
    > file in binary mode, the EOF character is 0A, while Windows apps
    > expect 0D0A. VisualBasic was not happy :)


    I'm sorry, but your statement of the alleged problem doesn't make much
    sense. Possibly you mean EOL (end of line), not EOF (end of file). In
    any case, when opening a file in binary mode, lines (and end-of-line
    conventions) are quite irrelevant -- you use a_string = f.read(nbytes)
    to read (or f.write(a_string) to write) as many bytes as you think you
    need, starting at the current position in the file.

    Perhaps if you provided more precise information than "VisualBasic was
    not happy", like: the exact code that you ran, and the exact [hint:
    use the repr() function] contents of the two input files and the
    output file, plus what you expected the contents of the output file to
    be, plus whatever VB had to say about the output file, we might be
    able to help you with the problem.

    Cheers,
    John
     
    John Machin, Dec 26, 2007
    #7
  8. On Wed, 26 Dec 2007 14:33:15 -0800, Dennis Lee Bieber
    <> wrote:
    >Works for me, as long as one uses a seek() call when switching
    >between read and write...


    Thanks, Dennis. Worked :) I just changed the access mode from binary
    to text so that Python handles the EOL character correctly, ie. CRLF
    for Windows instead of LF for *nix.

    For those interested, here's some working code in Windows +
    ActivePython:

    ==========
    import glob

    #activate.tmpl contains the stuff I want to append to each file
    f = open("activate.tmpl", "r")
    template = "\n\n" + f.read()
    f.close()

    for frm in glob.glob('*.frm'):
    f = open(frm, "r+")
    content = f.read()
    if 'Form_Activate' not in content:
    f.seek(0,2)
    f.write(template)
    f.close()
    ==========
     
    Gilles Ganault, Dec 27, 2007
    #8
    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. Brian Blazer

    Newbie append() question

    Brian Blazer, May 19, 2006, in forum: Python
    Replies:
    4
    Views:
    272
    Sybren Stuvel
    May 19, 2006
  2. HYRY
    Replies:
    10
    Views:
    608
    Bruno Desthuilliers
    Sep 26, 2007
  3. Alex Dowad
    Replies:
    4
    Views:
    273
    Michel Demazure
    May 1, 2010
  4. PerlFAQ Server
    Replies:
    0
    Views:
    390
    PerlFAQ Server
    Feb 24, 2011
  5. Fr
    Replies:
    1
    Views:
    70
    Evertjan.
    Apr 30, 2005
Loading...

Share This Page