@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 <raghavendra.gv.va...@gmail.com>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 with 'a+' mode/r+ mode. > > log = open('data.txt','r+/a+') > for x in log: > x = x.replace('Python','PYTHON') > print x, > log.close() > > It had properly written and replaced Python to PYTHON. > > Thanks for your suggestion. > > > > On Fri, Jan 22, 2010 at 12:42 PM, Shashwat Anand > <anand.shash...@gmail.com> wrote: > > 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 <raghavendra.gv.va...@gmail.com> > > wrote: > >> > >> Hi all, > >> > >> I have been trying to write a script where in it has to replace a > >> particular string in a file and write back to the file without intact > >> of the contents > >> > >> I have tried below mentioned steps, but of no avail: > >> > >> 1. Created a file by name data.txt and included some text (For > >> instance, > >> > >> ************************************************************************ > >> Then, I started searching for a good book on Python. I couldn't find > >> any! I did find some O'Reilly books > >> but they were either too expensive or were more like a reference > >> manual than a guide. So, I settled for > >> the documentation that came with Python. However, it was too brief and > >> small. It did give a good idea > >> about Python but was not complete. I managed with it since I had > >> previous programming experience, but > >> it was unsuitable for newbies. > >> ************************************************************************ > >> > >> 2.Read the file contents (For this opened the file in read mode -- log > >> = open('data.txt','r') > >> > >> 3.Iterated through the text using for loop for reading the contents in > >> the file -- for x in log: > >> > >> 4.Replaced the word Python with PYTHON as indicated --- x = > >> x.replace('Python','PYTHON') > >> > >> 6.printed the full contents -- It had printed the full contents by > >> replacing the word Python to PYTHON > >> > >> Python string did not get changed to PYTHON for the reasons that i did > >> not open the file in write mode and written the string > >> > >> [Query]: How to write Python string to PYTHON to that file without > >> intact of the full contents > >> > >> I was successful in routing the contents (with changed string to > >> PYTHON) to a different file. > >> > >> And i am aware that opening the file in write mode will wipe out the > >> previous contents. > >> > >> I have tried appending the content which it does but that is not my > goal. > >> > >> > >> 7. Closed the files outside for loop -- log.close() > >> -- > >> Raghavendra Vanam > >> _______________________________________________ > >> Tutor maillist - Tutor@python.org > >> To unsubscribe or change subscription options: > >> http://mail.python.org/mailman/listinfo/tutor > > > > > > > > -- > Raghavendra Vanam >
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor