Re: [Tutor] Replacing the string in a file

2010-01-22 Thread Andreas Kostyrka
The modes for updating files are + suffixed. r+ Open for reading and writing. The stream is positioned at the beginning of the file. w+ Open for reading and writing. The file is created if it does not exist, otherwise it is trun‐ cated. The stream is p

Re: [Tutor] Replacing the string in a file

2010-01-22 Thread Dave Angel
vanam wrote: Thanks for your mail. As you have suggested i have changed the mode to 'rw' but it is throwing up an error as below *** IOError: [Errno 22] invalid mode ('rw') or filename: 'data.txt' *** I am using python 2.6.4. But Script is managed to pass wit

Re: [Tutor] Replacing the string in a file

2010-01-22 Thread Shashwat Anand
and there is the everlasting : http://xkcd.com/353/ :D On Fri, Jan 22, 2010 at 2:16 PM, Stefan Behnel wrote: > Stefan Lesicnik, 22.01.2010 09:11: > > I have used the fileinput module for this quiet alot. It has an inplace > > option which does what you want and a backup option that keeps a back

Re: [Tutor] Replacing the string in a file

2010-01-22 Thread Stefan Behnel
Stefan Lesicnik, 22.01.2010 09:11: > I have used the fileinput module for this quiet alot. It has an inplace > option which does what you want and a backup option that keeps a backup. > > http://docs.python.org/library/fileinput.html > http://www.doughellmann.com/PyMOTW/fileinput/index.html Ah, r

Re: [Tutor] Replacing the string in a file

2010-01-22 Thread Shashwat Anand
@vanam: If you open the file again with only '+w' and not '+rw', I guess you'll not be getting this error - > IOError: [Errno 22] invalid mode ('rw') or filename: 'data.txt' On Fri, Jan 22, 2010 at 1:51 PM, vanam wrote: > Thanks for your mail. > > As you have suggested i have changed the mode

Re: [Tutor] Replacing the string in a file

2010-01-22 Thread vanam
Thanks for your mail. As you have suggested i have changed the mode to 'rw' but it is throwing up an error as below *** IOError: [Errno 22] invalid mode ('rw') or filename: 'data.txt' *** I am using python 2.6.4. But Script is managed to pass with 'a+' mode/r+ mo

Re: [Tutor] Replacing the string in a file

2010-01-22 Thread Stefan Lesicnik
> > vanam, 22.01.2010 07:44: > > > [Query]: How to write Python string to PYTHON to that file without > > intact of the full contents > > What one would normally do is: read the file (line by line if that's > ok in > your case), replace the word by the new one, write the new line to a > new > te

Re: [Tutor] Replacing the string in a file

2010-01-21 Thread Shashwat Anand
Editing original file can be risky at times. If @vanam wants to edit the same fie, he can open the original file, read it, rename it to say 'data.txt.bak' and then write the modified content to the file named as 'data.txt'. This way he gets the data in the file he wants and also have a backup of or

Re: [Tutor] Replacing the string in a file

2010-01-21 Thread Stefan Behnel
vanam, 22.01.2010 07:44: > [Query]: How to write Python string to PYTHON to that file without > intact of the full contents What one would normally do is: read the file (line by line if that's ok in your case), replace the word by the new one, write the new line to a new temporary file. When done,

Re: [Tutor] Replacing the string in a file

2010-01-21 Thread Shashwat Anand
You can try it by reading all data as string, replacing the string and then write it to target file via opeing it in +w mode. f = open('data.txt', 'rw').read().replace('Python', 'PYTHON') f1 = open('data.txt', 'w') f1.write(f) HTH On Fri, Jan 22, 2010 at 12:14 PM, vanam wrote: > Hi all, > > I