0

So lets say i was given a function to store whatever is in one filename to a different file name, how would i do that?

def return_newfile(self, filename):

    ......
    .....
    ....
    self.return_newfile(data)
JerryMichaels
  • 109
  • 1
  • 4
  • 9

1 Answers1

0

This is one way to do what I believe you want.

file1 = open("filename1","r")
file1_contents = file1.read()
file1.close()

file2 = open("filename2","w")
file2.write(file1_contents)
file2.close()

Now, there may be a better way to do what you want, like if you just want to copy a file without doing anything to the contents in between. However, if you want to process the contents, this is probably the way to go.

El'endia Starman
  • 2,204
  • 21
  • 35
  • 1
    I think this is a poor way to copy a file. It is both, technically inferior (exception unsafe, prone to memory exhaustion) and more complicated than using the ready-made `shutil.copyfile`. – 5gon12eder Nov 25 '15 at 03:02