Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Moving a Folder

00:00 In the previous lesson, you moved a file, and in this lesson, you’re going to move a folder, also using the .replace() method.

00:07 Now, you want to move this yearly/ directory from out of the notes/ directory into the plans/ directory so that your folder structure then looks like this.

00:16 You want to have it nested inside of plans/. And again, you can do this using the .replace() method. It moves both files and folders.

00:28 So, let’s first create the source path. So the source path is going to be notes_dir and then "yearly", because it’s currently living directly inside of notes/.

00:43 And then you want the destination, and the destination should be inside of the notes_dir, inside of the subdirectory called "plans".

00:53 And then it should keep the same name. So I’ll still call it "yearly". So your source is where it currently lives, the directory, and your destination is where you want to move it to.

01:07 And then you can perform the move by saying source.replace() and pass it the destination. Again, both source and destination need to be Path objects for this to work.

01:18 But once you call it, then you get as a return value the path of the new location of the resource that you moved. Now, let’s double-check that this actually worked.

01:30 And we want to see—make a plans_dir. is the the no—whoops—notes_dir

01:39 and then "plans". And then I can, again, just list the contents of this by saying plans_dir.iterdir() and pass that to the list() function.

01:54 Oops, that was one bracket too much. Copy this.

02:01 And here we are. Now the plans/ directory contains a folder called monthly/, a folder called yearly/ that you moved in there just now, and the weekly/ folder.

02:10 Now let’s just double-check that the yearly/ folder still contains all the text files that were initially in there.

02:20 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.

02:32 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.

02:51 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.

Avatar image for Dmitrii

Dmitrii on June 16, 2023

Hi, Martin!

I have a question. When we move a folder from one place to another, if the folder is already exist and not empty in the destination place - we have an exception “OSError: [Errno 66] Directory not empty”.

Is there any arguments like “exist_ok: True” to avoid the raise of the Exception? Or we have firstly check if the destination folder is already exist?

I checked the documentation and didn’t find anything. So maybe you know?

Thank you!

Dmitrii.

Become a Member to join the conversation.