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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Moving a File

00:00 In this lesson, you will learn how you can move files and folders using Python’s pathlib module. Now, if you consider the folder structure that you’ve worked with so far, it’s kind of not that nicely structured and not really ideal. So you have, for example, goals3.txt that sits inside of the plans/ directory, but you have goals1 and goals2 out here directly in the notes/ directory.

00:24 So, you might want to move goals3 out here to goals1 and 2. And also the yearly/ folder, it doesn’t really make that much sense to have it directly inside of your notes/ directory.

00:36 It makes more sense to have it in the subdirectory called plans/ together with monthly/ and /weekly at the same level.

00:43 So what you want to do in this lesson is you’re going to move the yearly/ directory inside of the plans/ directory, and you’re also going to move goals3.txt out of the plans/ directory and directly into the notes/ directory.

00:57 You can do all of these things with pathlib. Let’s head back over to IDLE and get started on doing that right away.

01:06 To move files and folders using pathlib, you can work with the .replace() method. And the .replace() method, you call it on a Path object that is your source path, and you pass it an argument that is going to be your destination path. So for this, I will have to build two paths.

01:23 Let’s start with that. We said we want to move goals3.txt from the plans/ subdirectory out into the notes/ directory.

01:32 So I want to say the old_path_goals_3let’s be very explicit here—is inside of the "notes" directory and then inside of the subdirectory called "plans", and then we have the filename that was "goals3.txt".

01:52 Let’s double-check on that. So it sits inside of /Users/<my username>/notes/ plans/goals3.txt. This is the current path of the file.

02:01 Now I want to move it outside, into the notes/ directory, but I need to create a new path for it that also includes the filename. So I’m going to say new_path_goals_3, not yet 4.

02:15 It’s going to be inside of the "notes" directory, "notes" directory. And then I need the filename, which is going to stay the same—I’m going to still call it "goals3.txt".

02:31 So you can see the new path. Whoops. new_path_goals_3 is going to be notes/ and then directly goals3.txt. And now I can use the .replace() method to move it from one path to the new path. I’m going to say old_path_goals_three.replace() and then pass it the target. So, this is the new path

02:57 for goals3. And once I execute this, you can see that I get as a return value, I get the new path of where I moved the file to in this case. And now just to double-check that, I’m going to use .iterdir() just to see what’s inside of notes_dir,

03:20 and as you can see, you have plans/, and goals3.txt has arrived. And we can also double-check that it’s not inside of plans/ anymore.

03:30 So if I list((notes_dir / "plans").iterdir())

03:43 then you can see it now only contains the two folders monthly/ and weekly/ but not the file anymore.

03:51 So by using .replace() on an old path and then passing it the new path as an argument—both of these need to be pathlib.Path objects—you can move a file from a source to a destination.

04:05 Keep in mind that if the new path already existed—so, if we already had a file called goals3.txt directly in the notes/ directory—then using .replace() would actually replace the new path with the old path.

04:20 So it would overwrite anything that is already there on the new path location. So that’s something to keep in mind. Okay, so you successfully moved a file.

04:29 In the next lesson, you’re going to move a folder.

akazimierz on Jan. 3, 2023

A remark: while doing this exercise an idea popped into my head – I checked whether new_path_goals_3 existed – without any surprise .exist() returned False. After moving the file the old_path_goals_3 ceased to exist, obviously.

James Tuttle on July 3, 2023

I did my own to move a file but get this error:

PermissionError: [WinError 5] Access is denied: 'C:\\Users\\otuttlej\\image1.png' -> 'C:\\Users\\otuttlej\\images'

Why am I having permission issues? I have admin..

Bartosz Zaczyński RP Team on July 4, 2023

@James Tuttle There could be multiple reasons. For example, you can open the same file using only one application on Windows. Make sure to close any programs that might be showing the picture and try again.

James Tuttle on July 6, 2023

@Bartosz Zaczyński Thank you for the replay and yes, a few ways to debug since on Windows.

Become a Member to join the conversation.