How to copy a file in Python?
Question: I need to copy a file specified by the user and make a copy of it (giving it a
name specified by the user). This is my code:

    
    
    import copy
    
    def main():
    
        userfile = raw_input('Please enter the name of the input file.')
        userfile2 = raw_input('Please enter the name of the output file.')
    
        infile = open(userfile,'r')
    
        file_contents = infile.read()
    
        infile.close()
    
        print(file_contents)
    
    
        userfile2 = copy.copy(file_contents)
    
        outfile = open(userfile2,'w+')
    
        file_contents2 = outfile.read()
    
        print(file_contents2)
    
    main()
    

Something strange is happening here, as it doesn't print the contents of the
second file, outfile.


Answer: If you are reading outfile, why do you open it with `'w+'`? This truncates the
file.

Use `'r'`to read. See the
[link](http://docs.python.org/2.7/library/functions.html#open)

