-2

I'm using shutil library. When I using :

backuped = shutil.copyfile(file1, file2)

file2 is empty. There is not any problem with shutil.move. How can I copy file without losing data?

TheNone
  • 5,684
  • 13
  • 57
  • 98

1 Answers1

1

Are you on Mac? Shutil does not work properly on Mac according to the PyScripter pop-up.

You also need to be more specific, I could not replicate this problem. However, I would suggest checking:

  • File Extensions
  • File Locations
  • File Metadata (this is what's lost when copying on a Mac, also, with Mac, shutil.move DOES work, so it may be that)

You COULD do it manually with files as I'll show below, however I think this way is slower than other built-in methods.

with open (file1, "rb") as f1:
    with open (file2, "wb") as f2:
        f2.writelines(f1.readlines())

This created an exact copy of the file with the location file1 to the location file2. This method works with all files and can be implemented with pickle to encrypt and serialise objects.

file2 does not need to already be a file either.

Frogboxe
  • 386
  • 4
  • 12
  • 1
    OP. Is this what you were looking for? If not just explain what it is that you need and I'll try to sort it out for you (if I know how that is, I'm no fan of sophistry). – Frogboxe Sep 29 '16 at 18:56