title: Python Replace content in specific line at txt file
body: <p>I have a txt file with content as below:
  name
  password
  number
  location</p>
  <p>the four items are separated into each line</p>
  <p>I would like to change the content in the specific line, eg:
  change the third line(&quot;number&quot;) to &quot;phone number&quot;</p>
  <p>Problem I faced is not replacing the &quot;number&quot;, &quot;number&quot; still there, and the &quot;phone number&quot; is appended at the last line</p>
  <pre><code>Output:
  name
  password
  number
  location
  phone number
  </code></pre>
  <p>How should I fix the code, so it can replace the content in certain line?</p>
  <p>Below is my code</p>
  <pre><code>userID = 1
  
  print(&quot;Modify: &quot;)
  print(&quot;1. Username&quot;)
  print(&quot;2. Password&quot;)
  print(&quot;3. Contact number&quot;)
  print(&quot;4. Location&quot;)
  print()
  print(&quot;Enter 'main' to back to main screen&quot;)
  select = int(input())
  change = input(&quot;Change to: &quot;)
  
  if select != 'main:':
      if int(select) &lt;= 4:
          #save to individual file
          fhand = open(&quot;user_&quot;+str(userID)+&quot;.txt&quot;,'r+') 
          
          lineNo = 0 #line number
          for line in fhand(): 
              lineNo = lineNo + 1
              if lineNo == select:
                  fhand.write(line.replace(line, change)) #replace the content
                  print()
                  print(&quot;Changed to:&quot;, change)
                  print(&quot;Change successful&quot;)
                  print(&quot;\n Bring you back to &lt;customer main menu&gt;&quot;)
   
          fhand.close()
  </code></pre>
  <p>I tried counting the line number, then when the line number reach the selection of the user, it starts to replace the line, and it doesn't work</p>
  <p>Thanks for the helps</p>