Moving a File
I will need to create a new directory and call it
images/ and then move the file
image1.png. If I’d be writing this inside of a script, I would be doing similar things to here that I’m just making like little short code comments that help me keep track of what I’m supposed to do here.
Move the file to this new directory. Okay, these are my two tasks, and let’s start with a) Create a new directory called `images` inside of the `my_folder` directory. Similar to what you did before, I’m going say
images_dir is going to be
And that’s just very helpful because it avoids making a typo, or maybe you don’t exactly know what the variable’s called, and then you can just cycle through it by pressing the Tab character. Here in the IDLE REPL, you see, like, sometimes it brings up this little box if I didn’t give it enough. Let me see.
m, yeah, here you go.
So double-check whether this looks correct.
Users/martin/my_folder/image1.png. Cool. Let’s just check whether it exists, that I didn’t mistype anything, because remember you can create paths even if the file doesn’t exist.
Now here you can see that
pathlib actually returns something, so there’s a bit of feedback if you want. It gives you back the
Path object that points to the new destination of the file, so where you just moved it … which means that if I’m now going to say
we’ll get a
True. Okay, but again, I still don’t entirely trust my command line, you know, so I want to go over to the graphical user interface and check whether it’s actually there and moved. Okay, you can see there’s already a space, a gaping hole where
image1.png used to be, but we have an
images/ folder, and inside of
image1.png. So the move worked out perfectly.
Become a Member to join the conversation.