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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cyberco

    #1

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

    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...

  • Peter Otten

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

    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.FileI nput(...)
    for line in fi:
    # process line
    fi.close()

    Personally, I use ordinary files instead.

    Peter

    Comment

    • cyberco

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

      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')
      =============== ============

      Comment

      • Fredrik Lundh

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

        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')
        =============== ============
        Source code: Lib/fileinput.py This module implements a helper class and functions to quickly write a loop over standard input or a list of files. If you just want to read or write one file see open...


        /.../ 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.writ e(text)

        </F>

        Comment

        • John Machin

          #5
          Re: fileinput.input ('test.txt') =&gt; ERROR: input() already active

          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.writ e(blahblah)

          HTH,
          John

          Comment

          • cyberco

            #6
            Re: fileinput.input ('test.txt') =&gt; ERROR: input() already active

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

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

            Comment

            • John Machin

              #7
              Re: fileinput.input ('test.txt') =&gt; ERROR: input() already active


              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.

              Comment

              Working...