[Tutor] Simple text file processing using fileinput module. "Grabbing successive lines" failure

Alan Gauld alan.gauld at btinternet.com
Mon Jul 2 19:50:38 CEST 2012


On 02/07/12 15:03, Flynn, Stephen (L & P - IT) wrote:
> Whilst having a play around with reading in textfiles and reformatting them I
 > tried to write a python 3.2 script to read a CSV file,


Best tool for csv files is the csv module, it covers most of the gotchas 
associated with such data.

> What I can't do is read the successive line to a short line in order to
 > append it onto the end of short line before writing the
 > entire amended line out.

Maybe so but we can't help with that because you haven't shown us any 
code related to that issue...

> I'm still thinking about how to persuade the fileinput module

fileinput is normally used when processing many similar files. Its not 
usually used when processing a single file. If you wanted to step onto 
the next file in the input list then fileinput would help there.
But processing lines within the file is up to you.

> I get a traceback as I'm obviously not using fileinput.FileInput.readline() correctly.

Nope, it doesn't look like it but you haven't posted enough code to be 
sure what is happening. But I'll take a guess...

> Traceback (most recent call last):
>    File "C:\Documents and Settings\flynns\workspace\PipeSmoker\src\pipesmoker\pipesmoker.py", line 35, in <module>
>      nextLine = fileinput.FileInput.readline(args.file)
>    File "C:\Python32\lib\fileinput.py", line 301, in readline
>      line = self._buffer[self._bufindex]
> AttributeError: 'str' object has no attribute '_buffer'

It looks like you are not creating an instance of the FileInput class.
You are trying to use the methods directly. Thus the class tries to 
execute the call by using args as self. But args is a string not a 
FileInput instance and it therefore finds no _buffer attribute.

Look at the documentation. The very first few lines show what you want:

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

The typical use is:
import fileinput
for line in fileinput.input():
     process(line)
---------------

Note the reference to processing a single file with open() and note the 
absence of FileInput in the example code.

Further down it says:

--------------
The class which implements the sequence behavior provided by the module 
is available for subclassing as well:

class fileinput.FileInput([files[, inplace[, backup[, mode[, openhook]]]]])

Class FileInput is the implementation; its methods filename(), fileno(), 
lineno(), filelineno(), isfirstline(), isstdin(), nextfile() and close() 
correspond to the functions of the same name in the module. In addition 
it has a readline() method which returns the next input line,
-----------------

So normally you don't need to use FileInput at all, unless you are 
creating some kind of specialized sub class version. But if you do
use it you need to use it like any other class and create an instance.

HTH,

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/





More information about the Tutor mailing list