fileinput.input('test.txt') => ERROR: input() already active

Discussion in 'Python' started by cyberco, Nov 20, 2006.

  1. cyberco

    cyberco Guest

    Using fileinput.input('test.txt') I probably forgot to process all
    lines or so, since I get the error 'input() already active' when i try
    to call fileinput.input('test.txt') again. But how can I 'close' the
    previous version I opened? I have no handle to it or so...
    cyberco, Nov 20, 2006
    #1
    1. Advertising

  2. cyberco

    Peter Otten Guest

    cyberco wrote:

    > Using fileinput.input('test.txt') I probably forgot to process all
    > lines or so, since I get the error 'input() already active' when i try
    > to call fileinput.input('test.txt') again. But how can I 'close' the
    > previous version I opened? I have no handle to it or so...


    fileinput.close()

    Or you forego the global FileInput instance with

    fi = fileinput.FileInput(...)
    for line in fi:
    # process line
    fi.close()

    Personally, I use ordinary files instead.

    Peter
    Peter Otten, Nov 20, 2006
    #2
    1. Advertising

  3. cyberco

    cyberco Guest

    Ah, thanks!
    Another related question I have: The following piece edits in place,
    but ads whitelines between all lines of a Windows text file. Why?

    ===========================
    fi = fileinput.input('test.txt', inplace=1)
    for l in fi:
    print l.replace('a', 'b')
    ===========================
    cyberco, Nov 20, 2006
    #3
  4. cyberco wrote:

    > Another related question I have: The following piece edits in place,
    > but ads whitelines between all lines of a Windows text file. Why?
    >
    > ===========================
    > fi = fileinput.input('test.txt', inplace=1)
    > for l in fi:
    > print l.replace('a', 'b')
    > ===========================


    http://docs.python.org/lib/module-fileinput.html

    /.../ lines are returned including the trailing newline when it
    is present.

    and "print" adds its own newline, of course. try

    print text,

    or

    sys.stdout.write(text)

    </F>
    Fredrik Lundh, Nov 20, 2006
    #4
  5. cyberco

    John Machin Guest

    cyberco wrote:
    > Ah, thanks!
    > Another related question I have: The following piece edits in place,
    > but ads whitelines between all lines of a Windows text file. Why?
    >
    > ===========================
    > fi = fileinput.input('test.txt', inplace=1)
    > for l in fi:


    Please don't use lowercase "L" as a variable name; it is too easily
    confused with the digit 1 in some fonts. Use meaningful names.
    > print l.replace('a', 'b')
    > ===========================

    fi = fileinput.input('test.txt', inplace=1)
    for line in fi:
    print line.replace('a', 'b')

    It's nothing to do with whether the file is a "Windows text file" or
    not. The reason is that line already has a "\n" i.e. newline character
    at the end. The print statement adds another.
    Either:
    (1) change
    print blahblah
    to
    print blahblah, # comma suppresses the added newline
    or
    (2) put
    import sys
    up the front and change
    print blahblah
    to
    sys.stdout.write(blahblah)

    HTH,
    John
    John Machin, Nov 20, 2006
    #5
  6. cyberco

    cyberco Guest

    > > Please don't use lowercase "L" as a variable name;

    Ohoh...I plead guilty! I totally forgot about CS 101... :)
    cyberco, Nov 20, 2006
    #6
  7. cyberco

    John Machin Guest

    cyberco wrote:
    > > > Please don't use lowercase "L" as a variable name;

    >
    > Ohoh...I plead guilty! I totally forgot about CS 101... :)


    I don't understand the reference to CS 101.

    It's quite simple: If you want people to bother reading your postings
    and help you, make them easy to comprehend. Otherwise they won't bother
    a second time.
    John Machin, Nov 20, 2006
    #7
    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. Replies:
    2
    Views:
    546
  2. Adam Funk
    Replies:
    4
    Views:
    423
    Adam Funk
    Dec 21, 2007
  3. kj
    Replies:
    0
    Views:
    306
  4. Przemyslaw Bak

    fileinput.input, readlines and ...

    Przemyslaw Bak, Jun 24, 2009, in forum: Python
    Replies:
    7
    Views:
    1,257
    Kushal Kumaran
    Jun 26, 2009
  5. carlos seramos
    Replies:
    2
    Views:
    477
    carlos seramos
    Aug 1, 2003
Loading...

Share This Page