Write a program in Python 3.5 that reads a file, then writes a different file with the same text that was in the first one as well as more?
<p>The exact question to this problem is:
*Create a file with a 20 lines of text and name it âlines.txtâ. Write a program to read this a file âlines.txtâ and write the text to a new file, ânumbered_lines.txtâ, that will also have line numbers at the beginning of each line.
Example: 
Input file: âlines.txtâ</p>

<pre><code>Line one
Line two
</code></pre>

<p>Expected output file:</p>

<pre><code>1 Line one
2 Line two
</code></pre>

<p>I am stuck, and this is what I have so far. I am a true beginner to Python and my instructor does not make things very clear. Critique and help much appreciated.</p>

<pre><code>file_object=open("lines.txt",'r')
for ln in file_object:
    print(ln)

count=1
file_input=open("numbered_lines.txt",'w')
for Line in file_object:
    print(count,' Line',(str))
    count=+1
file_object.close
file_input.close
</code></pre>

<p>All I get for output is the .txt file I created stating lines 1-20. I am very stuck and honestly have very little idea about what I am doing. Thank you</p>

<p>You have all the right parts, and you're almost there:</p>

<p>When you do</p>

<pre><code>for ln in file_object:
    print(ln)
</code></pre>

<p>you've exhausted the contents of that file, and you won't be able to read them again, like you try to do later on.</p>

<p>Also, <code>print</code> does not write to a file, you want <code>file_input.write(...)</code></p>

<p>This should fix all of that:</p>

<pre><code>infile = open("lines.txt", 'r')
outfile = open("numbered_lines.txt", 'w')

line_number = 1
for line in infile:
    outfile.write(str(line_number) + " " + line)

infile.close()
outfile.close()
</code></pre>

<p>However, here is a more pythonic way to do it:</p>

<pre><code>with open("lines.txt") as infile, open("numbered_lines.txt", 'w') as outfile:
    for i, line in enumerate(infile, 1):
        outfile.write("{} {}".format(i, line))
</code></pre>

python file