-1

I have some photos and some videos that I have to copy them in another folder without using shutil and os.rename. I can’t use shutil and os.rename because this is a condition for that Python exercise. I tried open but it only worked for text and not photos and videos.

LeopardShark
  • 3,820
  • 2
  • 19
  • 33
M.HBA
  • 51
  • 6

2 Answers2

2

open file in binary mode, and write in binary mode

original_file = open('C:\original.png', 'rb') # rb = Read Binary
copied_file = open('C:\original-copy.png', 'wb') #wb = Write Binary

copied_file.write(original_file.read())

original_file.close()
copied_file.close()

Python File Documentation

Dean Van Greunen
  • 5,060
  • 2
  • 14
  • 28
1

You can use pathlib:

from pathlib import Path

Path("old_file.png").write_bytes(Path("new_file.png").read_bytes())
LeopardShark
  • 3,820
  • 2
  • 19
  • 33