title: python file read, write line by line
body: <p>I am studying python file I/O. I made a simple program(<strong>main.py</strong>). </p>
  
  <p>My goal is read line by line and write line by line.</p>
  
  <pre><code>fstream = open("input2.txt", 'r');
  line = fstream.readline()
  while line:
      print(line);
      line = fstream.readline()
  
  fstream.close()
  </code></pre>
  
  <p>below are my <code>input2.txt</code> file</p>
  
  <pre><code>start.
  hello world.
  hello python.
  I am studying file I/O in python
  end.
  </code></pre>
  
  <p>when I run python program</p>
  
  <blockquote>
    <p>python main.py</p>
  </blockquote>
  
  <p>Then, result is ...</p>
  
  <pre><code>start.
  
  hello world.
  
  hello python.
  
  I am studying file I/O in python
  
  end.
  </code></pre>
  
  <p>That is not the same as I expected.</p>
  
  <p>So I modified the main.py</p>
  
  <pre><code>fstream = open("input2.txt", 'r');
  line = fstream.read().split("\n")
  while line:
  print(line);
  line = fstream.read().split("\n")
  
  fstream.close()
  </code></pre>
  
  <p>However my program diged into <strong>infinite loop.</strong></p>
  
  <p><a href="https://i.stack.imgur.com/oTy2O.png" rel="nofollow noreferrer">picture of infinite loop</a></p>
  
  <p>To solve this problem what should I do?</p>
  
  <hr>
  
  <p>The result I expected is the following.</p>
  
  <pre><code>start.
  hello world.
  hello python.
  I am studying file I/O in python
  end.
  </code></pre>