File I/O in Python
Question: I'm attempting to read a CSV file and then write the read CSV into another CSV
file.

Here is my code so far:

    
    
    import csv 
    with open ("mastertable.csv") as file:
        for row in file:
        print row  
    with open("table.csv", "w") as f:
       f.write(file)
    

I eventually want to read a CSV file write to a new CSV with appended data.

I get this error when I try to run it.

    
    
    Traceback (most recent call last):
    File "readlines.py", line 8, in <module>
    f.write(file)
    TypeError: expected a character buffer object
    

From what I understood it seems that I have to close the file, but I thought
with automatically closed it?

I'm not sure why I can write a string to text but I can't simply write a CSV
to another CSV almost like just making a copy by iterating over it.


Answer: To read in a CSV and write to a different one, you might do something like
this:

    
    
    with open("table.csv", "w") as f:
        with open ("mastertable.csv") as file:
            for row in file:
               f.write(row)
    

But I would only do that if the rows needed to be edited while transcribed.
For the described use case, you can simply copy it with
[shutil](https://docs.python.org/2/library/shutil.html) before hand then
opening it to append to it. This method will be much faster, not to mention
far more readable.

The `with` operator will handle file closing for you, and will close the file
when you leave that block of code (given by the indentation level)

