How to copy a file with Python and shutil ecc.

Here is some code to copy a file with shutil.

# copy a file

import shutil

# copy the file file.txt in the backup folder
shutil.copy("file.txt", "backup")

or this way …

with open("file.txt", "rb") as read:
	with open("backup/file2.txt", "wb") as filetobecopied:
		filetobecopied.write(read.read())

This is the video about these methods of copying files.


Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.