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 the default subtitles language 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 see our video player troubleshooting guide to resolve the issue.

Starting to Write the Script

00:00 Let’s get started with some Python scripting. Okay, so here’s my overall task, and I’m going to start by breaking it out into subtasks. So again, I’m going to want to import—from pathlib import Pathand then I want to create a subfolder called images/.

00:28 Then I want to move all image files to that folder.

00:36 So these are two tasks that I see here in the overall challenge. I already decided I want to use pathlib for this. I did the import, and now I’m going to get started on the first task, which is to create a subfolder called images/. Okay, subfolder to what? All right, we have a new folder in the practice_files/ folder. Okay, so I need access to the practice_files/ folder.

00:58 That’s another task that I need to do before: get path to the practice_files/ folder. And then I want to create a subfolder. And then I want to move all image files to that folder. Step one, get the path to the practice_files/ folder. Okay, so I already did that over here.

01:19 I said practice_dir = Path.home() / "python-basics-exercises" / "practice_files". So I’m just going to copy this code and put it into my script.

01:29 So this defines the path based on, like, that is located inside of my home directory and then inside of python_basics_exercises/ in a folder called—it’s a little off the screen, but there it is—practice_files/.

01:43 So that’s the path to the practice files, and I checked that it exists over here in my REPL. So that’s all good. Now in here I want to create a subfolder called images/.

01:53 So I’m going to start with defining the path. I’m going to say images_dir will be inside of practice_dir and then a folder called "images".

02:05 And then I want to say images_dir.mkdir().

02:11 Now I might want to put in here exist_ok=True if I want to run this more often. Let’s bump into this maybe. For now, I’m just going to keep it the way it is. images_dir.mkdir().

02:24 This assumes that the folder doesn’t exist yet, which I know for now it doesn’t. So I got this subfolder called images/ created.

Become a Member to join the conversation.