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.
Asked
Active
Viewed 629 times
-1

LeopardShark
- 3,820
- 2
- 19
- 33

M.HBA
- 51
- 6
-
which os are you using ? – Thierno Amadou Sow Oct 03 '21 at 08:31
2 Answers
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()

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