<div>
<br>
</div>
<div></div>
<p style="color: #A0A0A8;">On Thursday, 21 March 2013 at 13:00, Hedieh Ebrahimi wrote:</p>
<blockquote type="cite" style="border-left-style:solid;border-width:1px;margin-left:0px;padding-left:10px;">
<span><div><div><div dir="ltr"><div><div><div>Hi all, <br><br></div>I am trying to write the below code to be able to read from a text file and fine lines that start with <br></div>TRAN and then write this line and the next 2 following lines to another file called Output.txt.<br>
<br></div>my code is as below:<br><br><br><div>with open('p.txt') as f1:<br> with open('Output.txt', 'a') as f2:<br> for line in f1:<br> if line.startswith('TRAN'):<br> print("true")<br>
f2.write(line)<br> f2.write(f1.next())<br> f2.write(f1.next())<br><br></div><div>but
when i type python he.py in folder where the p.txt and Output.txt are
located, nothing happens and no data is written to the output file.<br>
<br></div><div>Could somebody please tell me where is my error?</div><div><br></div></div></div></div></span></blockquote><div><br></div><div>I tried your code and it worked fine for me. Here is my output: <a href="http://dpaste.com/1030136/">http://dpaste.com/1030136/</a></div><div><br></div><div>You can see the code, then input file, the output of it running (true printed three times) and then the output file contents.</div><div><br></div><div>The only thing I can think of is the file permissions. What output do you see when you run the code?</div><div>
<br>
</div>