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
  • 2,016
  • 2
  • 21
  • 30
Akshay Hazari
  • 3,186
  • 4
  • 48
  • 84
  • 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
  • 8,103
  • 8
  • 31
  • 65