fileinput

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Aug 13 07:20:00 CEST 2009


En Wed, 12 Aug 2009 01:27:47 -0300, naaman <arphaksad at gmail.com> escribió:

> I'm writing my first Python script and
> I want to use fileinput to open a file in r+ mode.
> Tried fileinput.input(sys.argv[1:],"r+") but that didn't work.
> ANy ideas?

Don't use fileinput, it can't handle r+, only sequencial access.
Even if you could use it, the documentation says [1]:
fileinput.input([files[, inplace[, backup[, mode[, openhook]]]]])

"r+" corresponds to the `inplace` parameter (behaving like inplace=True)  
instead of `mode`. You could write fileinput.input(sys.argv[1:],  
mode="r+") instead - but again from the docs: "mode... must be one of 'r',  
'rU', 'U' and 'rb'"

> Need to find and overwrite a line in a file several times.
> I can do it using open and seek() etc. but was wondering if I can use
> fileinput.

open+seek is the way to do that (assuming you don't alter the line length)

[1] http://docs.python.org/library/fileinput.html#fileinput.input

-- 
Gabriel Genellina




More information about the Python-list mailing list