Changing a line in a text file
How do I change a line in a file?? For example I have the follwing text in my file: line1 line2 line3 line4 How do I replace 'line2' with 'newline'. Since the write operation in python will overwrite everything. Regards, Kah -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing a line in a text file
kah wrote: How do I change a line in a file?? For example I have the follwing text in my file: line1 line2 line3 line4 How do I replace 'line2' with 'newline'. Since the write operation in python will overwrite everything. This is the best I can figure out what you mean: lines = [] for line in file(myfile.txt): if line == line2\n: line = newline\n lines.append(line) file(myfile.txt, W).writelines(lines) -- Michael Hoffman -- http://mail.python.org/mailman/listinfo/python-list
RE: Changing a line in a text file
Hi, Here is the program for replacing 'line2' with 'newline'. inp_file.txt contains, line1 line2 line3 line4 EOF #-- Program starts #Get the inp file in to a list inp_file_list = [x for x in open('inp_file.txt')] #modify the desired line, Note: you should include newline inp_file_list[1] = 'xxx\n' #Open the same file write_to_file = open('inp_file.txt', 'w') #write the list in to the file write_to_file.writelines(inp_file_list) #close the file write_to_file.close() #-- Program ends Hope someone will give better idea. HTH, Vishnu -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of kah Sent: Monday, April 25, 2005 3:09 PM To: python-list@python.org Subject: Changing a line in a text file How do I change a line in a file?? For example I have the follwing text in my file: line1 line2 line3 line4 How do I replace 'line2' with 'newline'. Since the write operation in python will overwrite everything. Regards, Kah -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing a line in a text file
Hi, the example provided by Vishnu is quite close to what I want but it still required me to write all the data back to the file. Is there any way where I can just write a particular line? Regards, Kah -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing a line in a text file
kah wrote: Hi, the example provided by Vishnu is quite close to what I want but it still required me to write all the data back to the file. Is there any way where I can just write a particular line? If you are asking whether you can update a file in place, the answer is yes - look up lseek in the Python documentation (module os, under File Descriptor Operations. However, you asked about replacing one line with another of a different length: since this will mean changing the offsets of all subsequent bytes you have no way to do this other than writing out the whole content of the file following the modification. You would also have to ensure that you truncated the file to the correct length. In general, although they don't make it obvious that they are doing so most programs that change files (text editors and the like) are really writing new copies. regards Steve -- Steve Holden+1 703 861 4237 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ Python Web Programming http://pydish.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing a line in a text file
Hello kah, How do I change a line in a file?? For example I have the follwing text in my file: line1 line2 line3 line4 How do I replace 'line2' with 'newline'. Since the write operation in python will overwrite everything. See http://docs.python.org/lib/module-fileinput.html (inplace=1 is what you want). Bye. -- Miki Tebeka [EMAIL PROTECTED] http://tebeka.bizhat.com The only difference between children and adults is the price of the toys pgpWYGsd0cadw.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing a line in a text file
Steve Holden [EMAIL PROTECTED] writes: kah wrote: However, you asked about replacing one line with another of a different length: since this will mean changing the offsets of all subsequent bytes you have no way to do this other than writing out the whole content of the file following the modification. You would also have to ensure that you truncated the file to the correct length. In general, although they don't make it obvious that they are doing so most programs that change files (text editors and the like) are really writing new copies. In addition, I would argue that editing a file in place using a non-interactive program is dangerous and bad practice in general. By the time you find a bug in your edit script, the original is lost. This is something I learned from bitter experience when I tried to be smart and make script-based edits over entire directories of html files. In unix shell scripting idiom, I would do something like: mv file file.bak sed -e 'g/oldline/c newline' file.bak file And yes, I know that some versions of sed have the --in-place option. Then, I would check for side effects: diff file file.bak All of this can be done in python, however I'm not overly familiar with difflib and it seems to require both versions of the file in memory. So an external diff might be better. import os os.rename(foo,foo.bak) infile = open(foo.bak,'r') outfile = open(foo,'w') for line infile: #test and substitution code block outfile.write(line) Using separate input and output files also has the advantage of being memory efficient. regards Steve -- Steve Holden+1 703 861 4237 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ Python Web Programming http://pydish.holdenweb.com/ -- Kirk Job-Sluder The square-jawed homunculi of Tommy Hilfinger ads make every day an existential holocaust. --Scary Go Round -- http://mail.python.org/mailman/listinfo/python-list