Moving a Folder
And this is how you can move files and folders using Python’s
pathlib module. You use the
.replace() method to move files or folders from a source to destination path. You’ve seen this happen like that.
You define a source
Path object, so the path to where you want to pick up a file or a folder, and then a destination, which is where you want to move it to, and then say
source.replace(destination), and it returns the
Path object that points to the destination of where you moved to file or folder to.
Keep in mind that that
.replace() will overwrite the destination path if it already exists. And if you want to be more clear about deleting files and folders, there’s other ways to do this than just overwriting them, and you’ll learn how to do that in the next lessons.
Become a Member to join the conversation.