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

Python Basics Exercises: File System Operations (Summary)

In Python Basics: File System Operations, you learned how to use Python to work with files and folders. As a programmer, you’ll use the pathlib and shutil modules to complete file system operations. These include creating, iterating over, searching for, moving, and deleting files and folders.

While you already got lots of hands-on practice with file system operations, programmers never stop practicing! The more you use your new skills, the more comfortable you’ll be when it’s time to put them to work in your own coding. That’s why it’s a great idea to complete exercises to reinforce and test your knowledge of the file system.

In this video course, you’ve practiced:

  • Creating a directory
  • Iterating over the contents of a directory
  • Searching for files using wildcards
  • Moving and deleting files and folders

To learn more about file system operations in Python, check out Real Python’s tutorial and video course on pathlib.

If you found this course useful, then check out the other Python Basics courses and the Python Basics: A Practical Introduction to Python 3 book.

Download

Sample Code (.zip)

23.2 KB
Download

Course Slides (.pdf)

3.3 MB

00:00 Congrats! You made it to the end of the challenge as well, and you wrote a script to move all image files to a new directory. The task was to take this practice_files/ folder that you can download from the supporting material, and there was the old folder structure where you had image files spread all over the folder structure, and your task was to gather all of these—there are specifically four image files—and move them to a new folder called images/.

00:27 Now you did that, and the new folder structure looked like that. All of the image files from inside of documents/ are now inside of a separate folder called images/ that is a sibling folder to documents/.

00:38 So nice work with that, and if you achieved it in a different way than I did, that is completely fine. Like I said, there’s multiple ways of solving this challenge, and I hope that seeing me walk you through this was a helpful exercise to give you maybe an alternative view or help you out if you got stuck somewhere, and especially also give you some tips and approaches on how you can tackle such a code challenge.

01:00 Those tips that you saw me apply in these lessons were to use code comments to help you get organized, then to break out the challenges into smaller tasks, tackle each of those tasks at a time, don’t worry if you want to look up some documentation on the way if you need some help, also to keep testing repeatedly to see whether the code does what you expected it to do.

01:23 And I also created a bit of a tiny testing framework where I which just copied the original file structure so that it wouldn’t get damaged during the tests. That’s it.

01:34 You’re at the end of this Python Basics practice course. You practiced using Python to create a directory, iterate over the contents of a directory, search for files using wildcards, and move and delete files and folders.

01:46 So good job, and if you want to learn more, remember there’s this pathlib course and article that we have on the site where you can learn a lot more about using Python 3’s pathlib module.

02:00 That’s it. Thanks for joining, and congratulations for making it to the end of this practice course. And I want send you off with saying, keep practicing because using your fingers and your coding brain and your typing skills is really what’s going to train you to become a better programmer.

02:18 And yeah, it takes practice, so keep practicing. Glad to have you here and hope to see you around Real Python.

Avatar image for Hans Geukens

Hans Geukens on Jan. 23, 2023

Thanks for the training video. Are you sure that the doc string correctly covers the script content? I believe it generates the images-folder anyway, even if it already existed before, right?

Avatar image for Martin Breuss

Martin Breuss RP Team on Jan. 24, 2023

@Hans Geukens glad it’s been useful! :)

The docstring works the way it is. The exist_ok parameter means that Python will only attempt to create the directory if it doesn’t already exist, it’s kinda like saying:

if not folder_name.exists():
    folder_name.mkdir()

So it won’t create the directory if it’s already there, but it will avoid throwing an error that would come up if you didn’t use the parameter.

If you want, you can check out the lesson on Creating a Directory in the base course or the documentation on Path.mkdir() for more info.

Become a Member to join the conversation.