fileinput

N

naaman

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?

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.

thanks;
 
D

Dave Angel

naaman said:
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?

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.

thanks;
I haven't used it, but check out the 'inplace' keyword parameter.

DaveA
 
N

naaman

naaman said:
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?
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.

I haven't used it, but check out the 'inplace' keyword parameter.

DaveA

I've only Python for a week so I'm not sure what inplace does
 
G

Gabriel Genellina

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
 
N

naaman

naaman said:
naaman wrote:
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?
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.
thanks;
I haven't used it, but check out the 'inplace' keyword parameter.
DaveA
I've only Python for a week so I'm not sure what inplace does

You should read the docs for it
    (http://www.python.org/doc/2.6.2/library/fileinput.html ),
but it's not very clear to me either  So I dug up an example on the web:
     (ref:  http://effbot.org/librarybook/fileinput.htm)

import fileinput, sys

for line in fileinput.input(inplace=1):
    # /convert Windows/DOS text files to Unix files/
    if line[-2:] == "\r\n":
        line = line[:-2] + "\n"
    sys.stdout.write(line)

The inplace argument tells it to create a new file with the same name as
the original (doing all the necessary nonsense with using a scratch
file, and renaming/deleting) for each file processed.  Stdout is pointed
to that new version of the file.  Notice that you have to explicitly
write everything you want to wind up in the file -- if a given line is
to remain unchanged, you just write "line" directly.

If you're new to Python, I do not recommend trying to do open/seek to
update a text file in place, especially if you're in DOS.  There are
lots of traps.  the inplace method of fileinput avoids these by
implicitly creating temp files and handling the details for you, which
probably works great if you're dealing with text, in order.

DaveA

Thanks Dave. I'll check that out
 
N

naaman

naaman said:
naaman wrote:
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?
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.
thanks;
I haven't used it, but check out the 'inplace' keyword parameter.
DaveA
I've only Python for a week so I'm not sure what inplace does

You should read the docs for it
    (http://www.python.org/doc/2.6.2/library/fileinput.html ),
but it's not very clear to me either  So I dug up an example on the web:
     (ref:  http://effbot.org/librarybook/fileinput.htm)

import fileinput, sys

for line in fileinput.input(inplace=1):
    # /convert Windows/DOS text files to Unix files/
    if line[-2:] == "\r\n":
        line = line[:-2] + "\n"
    sys.stdout.write(line)

The inplace argument tells it to create a new file with the same name as
the original (doing all the necessary nonsense with using a scratch
file, and renaming/deleting) for each file processed.  Stdout is pointed
to that new version of the file.  Notice that you have to explicitly
write everything you want to wind up in the file -- if a given line is
to remain unchanged, you just write "line" directly.

If you're new to Python, I do not recommend trying to do open/seek to
update a text file in place, especially if you're in DOS.  There are
lots of traps.  the inplace method of fileinput avoids these by
implicitly creating temp files and handling the details for you, which
probably works great if you're dealing with text, in order.

DaveA

I wonder if inline creates an empty file and then the text from the
processed file gets written to it. If that's what is going on then
it's perfect solution. It will eliminate all the tell() and seek() and
overwriting a shorter line with a longer one. I'll try it over the
weekend. thanks
 
N

naaman

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

unfortunately, I am overwriting shorter with longer
 
N

naaman

naaman said:
naaman wrote:
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?
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.
thanks;
I haven't used it, but check out the 'inplace' keyword parameter.
DaveA
I've only Python for a week so I'm not sure what inplace does

You should read the docs for it
    (http://www.python.org/doc/2.6.2/library/fileinput.html ),
but it's not very clear to me either  So I dug up an example on the web:
     (ref:  http://effbot.org/librarybook/fileinput.htm)

import fileinput, sys

for line in fileinput.input(inplace=1):
    # /convert Windows/DOS text files to Unix files/
    if line[-2:] == "\r\n":
        line = line[:-2] + "\n"
    sys.stdout.write(line)

The inplace argument tells it to create a new file with the same name as
the original (doing all the necessary nonsense with using a scratch
file, and renaming/deleting) for each file processed.  Stdout is pointed
to that new version of the file.  Notice that you have to explicitly
write everything you want to wind up in the file -- if a given line is
to remain unchanged, you just write "line" directly.

If you're new to Python, I do not recommend trying to do open/seek to
update a text file in place, especially if you're in DOS.  There are
lots of traps.  the inplace method of fileinput avoids these by
implicitly creating temp files and handling the details for you, which
probably works great if you're dealing with text, in order.

DaveA

here's the solution

import fileinput, sys

for line in fileinput.input(sys.argv[1],inplace=1):
if (line[:-1]==r'drew'):
line=line.replace(line,"fancy dog")
sys.stdout.write(line)

I want to replace drew in my input file with fancy dog.
Tested with this input file
angel
heaven
flying monkees
lazy dogs
drew
blue sky
veritas

and got this
angel
heaven
flying monkees
lazy dogs
fancy dog
blue sky
veritas

So drew was replaced with fancy dog.
Thanks to your inputs I got this solved. :))
 
N

naaman

naaman said:
naaman wrote:
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?
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.
thanks;
I haven't used it, but check out the 'inplace' keyword parameter.
DaveA
I've only Python for a week so I'm not sure what inplace does
You should read the docs for it
    (http://www.python.org/doc/2.6.2/library/fileinput.html ),
but it's not very clear to me either  So I dug up an example on the web:
     (ref:  http://effbot.org/librarybook/fileinput.htm)
import fileinput, sys
for line in fileinput.input(inplace=1):
    # /convert Windows/DOS text files to Unix files/
    if line[-2:] == "\r\n":
        line = line[:-2] + "\n"
    sys.stdout.write(line)
The inplace argument tells it to create a new file with the same name as
the original (doing all the necessary nonsense with using a scratch
file, and renaming/deleting) for each file processed.  Stdout is pointed
to that new version of the file.  Notice that you have to explicitly
write everything you want to wind up in the file -- if a given line is
to remain unchanged, you just write "line" directly.
If you're new to Python, I do not recommend trying to do open/seek to
update a text file in place, especially if you're in DOS.  There are
lots of traps.  the inplace method of fileinput avoids these by
implicitly creating temp files and handling the details for you, which
probably works great if you're dealing with text, in order.

here's the solution

import fileinput, sys

for line in fileinput.input(sys.argv[1],inplace=1):
    if (line[:-1]==r'drew'):
        line=line.replace(line,"fancy dog")
    sys.stdout.write(line)

I want to replace drew in my input file with fancy dog.
Tested with this input file
angel
heaven
flying monkees
lazy dogs
drew
blue sky
veritas

and got this
angel
heaven
flying monkees
lazy dogs
fancy dog
blue sky
veritas

So drew was replaced with fancy dog.
Thanks to your inputs I got this solved. :))

oops there should be a +"\n" after "fancy dog"
line=line.replace(line,"fancy dog"+"\n")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,226
Latest member
KristanTal

Latest Threads

Top