0

I have to read a line from a file do some computation and write to another file. I have done something like this

fd= open("abc.txt","w")
for line in open("test.txt","r"):
     Do something Here
     fd.write(modifiedline)

I have usually used with open and for open for line by line operations. Is the above way which I am using ok to use or is there a better way where we use for open and with open together.

I am a student and wish to know more. Any help is appreciated.

Chamath
  • 1,778
  • 2
  • 19
  • 30
Akshay Hazari
  • 2,995
  • 3
  • 40
  • 76
  • read [this](http://stackoverflow.com/questions/7753899/python-best-way-to-read-a-file-and-break-out-the-lines-by-a-delimeter). – Chamath Jul 09 '15 at 10:46

1 Answers1

5

Code

with open("abc.txt", 'w') as outfi,open("test.txt","r") as infil:
    for line in infil:
        Do something Here
        outfi.write(modifiedline)
The6thSense
  • 7,631
  • 6
  • 30
  • 63