-1

So I did:

def fcopy(original, copy):
'creates a copy of file original named copy'
    infile = open(original)
    content = infile.read()
    print(content)

But this only print out what's in the original file. No clue how to copy the content of original to the copy file.

Frank
  • 339
  • 8
  • 18
  • 2
    First you can just use [shutil](https://stackoverflow.com/questions/123198/how-do-i-copy-a-file-in-python). For this though you need to [open the copy file and write to it](https://stackoverflow.com/questions/5214578/python-print-string-to-text-file) – LinkBerest Oct 01 '15 at 02:02
  • @JGreenwell I think you can post this as an answer, this maybe what's OP looking for. – Remi Guan Oct 01 '15 at 02:03

1 Answers1

2

You need to open another file for writing and write to that one. The new file does not need to exist beforehand. I recommend you read this for an overview of file handling: https://docs.python.org/2/tutorial/inputoutput.html

As far as writing a function for this goes, here's a function that should work (note that this overwrites the file you're copying to):

def fcopy(original_file, copy_file):
    with open(original_file, 'r') as readf, open(copy_file, 'w') as writef:
        writef.write(readf.read())
btmcnellis
  • 662
  • 4
  • 10