fileinput

naaman arphaksad at gmail.com
Thu Aug 13 23:25:02 EDT 2009


On Aug 13, 1:20 am, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> En Wed, 12 Aug 2009 01:27:47 -0300, naaman <arphak... 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

unfortunately, I am overwriting shorter with longer



More information about the Python-list mailing list