fileinput module doesn't work to spec?

Dale Strickland-Clark dale at out-think.NOSPAMco.uk
Tue Oct 24 11:40:26 EDT 2000
According to the spec, the fileinput module should support overwriting
of the input file after renaming it to preserve a backup. (Extract
below.)

It doesn't seem to work.

1. The input file is overwritten with an empty file.
2. No backup is taken
3. stdout is written to the console in stead of the input (now output)
file

Apart from that, it's great!

Is this a recognised problem or am I doing something stoopid?

Thanks for any wisdom.


Extract from doc:

Optional in-place filtering: if the keyword argument inplace=1 is
passed to input() or to the FileInput constructor, the file is moved
to a backup file and standard output is directed to the input file.
This makes it possible to write a filter that rewrites its input file
in place. If the keyword argument backup='.<some extension>' is also
given, it specifies the extension for the backup file, and the backup
file remains around; by default, the extension is '.bak' and it is
deleted when the output file is closed. In-place filtering is disabled
when standard input is read. 

My Python:

# Tab delimited file to basic HTML Table converter

import sys, fileinput

def genCell(cell):
	if cell:
		if cell[0] == '"':
			cell = cell[1:-1]
		cell = cell.strip()
	if not cell:
		cell = ' '
	return ['<TD>',	cell, '</TD>']

def genRow(line):
	row = ['<TR>']
	for cell in line.split('\t'):
		row = row + genCell(cell)
	row.append('</TR>\n')
	return row

table = []
for line in fileinput.input(sys.argv[1:], inplace = 1):
	table = table + genRow(line)

	
print ''.join(table)




	

Dale Strickland-Clark
Out-Think Ltd
Business Technology Consultants





More information about the Python-list mailing list