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: File System Operations (Summary)

Now you’re ready to use Python for file system operations! This is a huge step up. Working with files allows you to hold onto your data after a program terminates, load your data more quickly and conveniently, and move your data from one computer to another.

In this video course, you’ve learned how to:

  • Create files and directories
  • Iterate over the contents of a directory
  • Search for files and folders using wildcards
  • Move and delete files and folders

To reinforce what you’ve learned here, complete the quiz in the next lesson. Then, head over to Python Basics Exercises: File System Operations.

Then, keep growing your skill set as a programmer by continuing with the other Python Basics courses and getting yourself a copy of Python Basics: A Practical Introduction to Python 3.

For more information on the topics in this course, check out:

Download

Sample Code (.zip)

22.5 KB
Download

Course Slides (.pdf)

6.0 MB
Avatar image for Ray Osborn

Ray Osborn on Dec. 20, 2022

Very clear course. I am a little confused about shutil compatibility with path-like objects in older Python versions. It looks as if path-like objects could only be used as shutil arguments in v3.8, but the Python documentation seems to imply that the move function was only updated in 3.9. I guess that doesn’t affect this course, because we would use the pathlib replace function, but unfortunately many shared Python distributions still run 3.7.

Avatar image for akazimierz

akazimierz on Jan. 5, 2023

Thanks for the course, Martin – I’m glad I learned new things.

Some time ago I wrote utility functions for IDLE, so I could move through directories as within the terminal – now perhaps I could refactor some code, or add new functions. IDLE ls() example

Avatar image for Martin Breuss

Martin Breuss RP Team on Jan. 5, 2023

@akazimierz that looks like a useful utility function. Do you have these somewhere on a GitHub repo and do you want to share them?

Avatar image for writetomdw

writetomdw on Jan. 25, 2023

nicely constructed course for beginners although it has references to generators and tuples so good to leave some supporting materials or links to read for absolute beginners to fit the title of this course. Overall it is a good course to clear the basic concepts of file system.

Avatar image for tonypy

tonypy on Feb. 21, 2023

The supporting material is very sparse, basically only has the exercises, so not of any use. It would be better if the key slides were included for reference, as is included in most other courses. Can that be updated?

Avatar image for tonypy

tonypy on Feb. 21, 2023

I apologise, I was looking at the pb-filesystem-exercises.pdf which I had downloaded. I did get the course slides pb-filesystem_JO2FIPC.pdf so all is ok. Many thanks.

Avatar image for tonypy

tonypy on Feb. 21, 2023

A question on error handling when removing empty and non-empty directories. With files we saw that .unlink(missing_ok=True) covers that position. I’ve looked and cannot find a corresponding simple entry for similarly managing folders. Is there one or is the only way to check whether the path exists?

Avatar image for Martin Breuss

Martin Breuss RP Team on Feb. 22, 2023

@tonypy not totally sure I understand what you’re asking, it seems that there are two concepts that you’re maybe mixing together?

  1. Deleting empty and non-empty directories
  2. Deleting existing empty directories, and continuing execution if a directory doesn’t exist

For the empty/non-empty directories situation, there are examples in the lessons on deleting an empty folder and deleting a non-empty folder, so I think you were wondering about point two, which also makes sense because you’re mentioning missing_ok from .unlink().

For deleting existing empty directories, you work with .rmdir(). You can see in the documentation that .rmdir() doesn’t take any arguments. If you take a look into the source code, then you can see that all the method does is calling os.rmdir() on the Path object.

The Python documentation on os.rmdir() shows that the function raises a FileNotFound error if the directory doesn’t exist, which you’ve probably seen and you can confirm in a REPL session:

>>> from pathlib import Path
>>> Path.home().joinpath("not-there").rmdir()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/martin/.pyenv/versions/3.11.0/lib/python3.11/pathlib.py", line 1156, in rmdir
    os.rmdir(self)
FileNotFoundError: [Errno 2] No such file or directory: '/Users/martin/not-there'

So, one way to manage the control flow around this situation is that you can handle that exception:

try:
    your_directory_path_object.rmdir()
except FileNotFoundError:
    pass

This try...except block essentially reproduces the functionality described in #2 above: It’ll delete existing empty directories, and continuing execution if a directory doesn’t exist.

Tl;DR: No, .rmdir() doesn’t have such an argument, but because it raises a specific exception, you can catch that exception and write code that has the same functionality as what .unlink(missing_ok=True) gives you for files, also without checking whether the path exists.

Avatar image for tonypy

tonypy on Feb. 22, 2023

Many thanks for the feedback. In summary, there isnt a nice simple entry that can be included like “exist_ok=True”. I have therefore used the following to preclude the occurrence of the error where no directory exists:

if old_dir.exists():
    shutil.rmtree(old_dir)
Avatar image for Martin Breuss

Martin Breuss RP Team on Feb. 22, 2023

@tonypy ah if you want to use shutil.rmtree() then it looks like you can use ignore_errors=True:

shutil.rmtree(old_dir, ignore_errors_True)

However, I’d advise you to be very careful with this function, because it can really delete a lot at once! It’s best to be very conservative with using it.

Avatar image for tonypy

tonypy on Feb. 22, 2023

Thanks for the update and warning.

Avatar image for Mark de Lange

Mark de Lange on July 11, 2023

Before I started this course, I was a bit confused by 4 Python Packages: os, sys, pathlib and shutil. When do you need one of these packages, as for instance os and pathlib seem to overlap a lot. Now I learned a lot about pathlib, next are the other packages. It may be handy to create an additional course about Python packages: how to get your best modules pick for the task (file system related)

Become a Member to join the conversation.