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>Good first try, and with that, I can go through your code and explain what you did right (or wrong)</p>

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

<p>This is fine, though generally you want to put a space before and after assignments (you are assigning the results of <code>open</code> to <code>file_object) and add a space after a</code>,` when separating arguments, so you might want to write that like so:</p>

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

<p>However, at this point the internal reference in the <code>file_object</code> have reached the end of the file, so if you wish to reuse the same object, you need to <a href="https://docs.python.org/3/library/io.html#io.IOBase.seek" rel="nofollow"><code>seek</code></a> back to the beginning position, which is <code>0</code>.  As your assignment only states write to the file (and not on the screen), the above loop should be omitted from the file (but I get what you want to do, you want to see the contents of the file immediately though sometimes instructors are pretty strict on what they accept).  Moving on:</p>

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

<p>Looks pretty normal so far, again, minor formatting issues.  In Python, typically we name all variables lower-case, as names with Capitalization are generally reserved for class names (if you wish to, you may <a href="https://docs.python.org/3/tutorial/classes.html" rel="nofollow">read about them</a>).  Now we enter into the loop you got</p>

<pre><code>    print(count,' Line',(str))
</code></pre>

<p>This prints not quite what you want.  as <code>' Line'</code> is enclosed inside a quote, it is treated as a string literal - so it's treated literally as text and not code.  Given that you had assigned <code>Line</code>, you want to take out the quotes.  The <code>(str)</code> at the end simply just print out the string object and it definitely is not what you want.  Also, you forgot to specify the <code>file</code> you want to print to.  By default it will print to the screen, but you want to print it to the the <code>numbered_lines.txt</code> file which you had opened and assigned to <code>file_input</code>.  We will correct this later.</p>

<pre><code>    count=+1
</code></pre>

<p>If you format this differently, you are assigning <code>+1</code> to <code>count</code>.  I am guessing you wanted to use the <code>+=</code> operator to increment it.  Remember this on your quiz/tests.</p>

<p>Finally:</p>

<pre><code>file_object.close
file_input.close
</code></pre>

<p>They are meant to be called as functions, you need to invoke them by adding parentheses at the end with arguments, but as <code>close</code> takes no arguments, there will be nothing inside the parentheses.  Putting everything together, the complete corrected code for your program should look like this</p>

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

<p>Run the program.  You will notice that there is an extra empty line between every line of text.  This is because by default the <a href="https://docs.python.org/3/library/functions.html#print" rel="nofollow"><code>print</code></a> function adds a new line <code>end</code> character; the line you got from the file included a new-line character at the end (that's what make them lines, right?) so we don't have to add our own here.  You can of course change it to an empty string.  That line will look like this.</p>

<pre><code>    print(count, line, file=file_input, end='')
</code></pre>

<p>Naturally, other Python programmers will tell you that there are Pythonic ways, but you are just starting out, don't worry too much about them (although you can definitely pick up on this later and I highly encourage you to!)</p>

python file