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

The Challenge: Moving All Image Files to a New Directory

00:00 So you ready for a challenge? This challenge is to move all image files to a new directory.

00:06 So when you download the code resources under supporting material, then you can find in there a folder that’s called practice_files/, and it has a subfolder that’s called documents/.

00:17 And then the documents/ directory contains several files and subfolders. And some of them are image files. They are going to have the endings .png, .gif, or .jpg.

00:29 Once you’ve downloaded this folder, go ahead and inspect it. Click around in there. It should look something like this. So you have the practice_files/ folder, then a documents/ subfolder, and then there’s a bunch of stuff in there. And among other things, there are image files. For example, here is image3.png, image4.jpg, image2.gif, and image1.png.

00:56 Now your task is to create a new folder in the practice_files/ folder and call it images/. And then you want to move all the image files into that folder.

01:05 So once you’re done, the new folder should have four files in it, the ones that I just mentioned before and that are listed here on the file again. Of course, your challenge is to do all of this with Python’s pathlib module.

01:17 Here’s a couple of tips to take along on your way.

01:21 Just like before, use code comments to help you get organized. Break out the challenges in smaller tasks, then tackle one task at a time. Look up any documentation if you need help.

01:34 You can go to the pathlib documentation or go back to the course. You also want to test repeatedly to see whether your code actually does what you expect it to do. So that could be using pathlib, or you can use your graphical user interface to see whether what you expect to happen actually happens. So those are some tips to take along on the way.

01:54 And now again, I will suggest you to stop here and don’t continue with the course. Try to tackle this challenge yourself. See if you can make it to the end, if you can complete it successfully. If you do need help, try a little before you actually go and look up the lessons.

02:12 But then you can always go to the next lesson and then watch me how I would solve this challenge. And keep in mind that that’s just a different way of doing it.

02:20 If you solved it differently, that is completely fine. There’s always many, many different ways that you can tackle a challenge, and comparing your own approach to someone else’s approach can be a helpful way to learn and train your coding skills. So get started.

02:36 Prepare for the challenge and tackle it. And once you’re done, move on to the next lesson and then watch over me solving the challenge.

Avatar image for ajackson54

ajackson54 on Aug. 23, 2024

Hello, Martin—I’m having problems with my practise_files/ folder. I keep getting a false response when I check for .exists(). When I downloaded this, I put it in my home directory. Here is my code:

>>> from pathlib import Path
>>> practise_files_dir = Path.home() / 'python-basics-exercises' / 'practise_files'
>>> practise_files_dir
WindowsPath('C:/Users/Afj67/python-basics-exercises/practise_files')
>>> practise_files_dir.exists()
False
>>> basics = Path.home() / 'python-basics-exercises'
>>> basics.exists()
True
>>> practise_files_dir = Path.home() /'_MACOSX'/ 'python-basics-exercises' / 'practise_files'
>>> practise_files_dir
WindowsPath('C:/Users/Afj67/_MACOSX/python-basics-exercises/practise_files')
>>> practise_files_dir.exists()
False

As you can see, I was getting desperate and tried to use ‘_MACOSX’ in my path.

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Aug. 23, 2024

@ajackson54 It looks like you made a typo in your folder name. The correct spelling is practice_files/ instead of practise_files/. That’s why Python finds the parent folder but not the specified subfolder.

Avatar image for ajackson54

ajackson54 on Aug. 24, 2024

Oh no!!! I thought it might have been something to do with the move from downloads to my home directory. Well, that’s a relief. Thank you, Bartosz.

Become a Member to join the conversation.