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:
- Python 3’s pathlib Module: Taming the File System - Video Course
- Python 3’s pathlib Module: Taming the File System - Written Tutorial
- Working With Files in Python - Video Course
- Working With Files in Python - Written Tutorial
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00
This lesson is a summary and includes additional resources for you to continue your learning. In the course, you learned how to use Python’s pathlib
module to create files and directories, iterate over the contents of a directory, search for files and folders using wildcards, and also to move and delete files and folders—a whole bunch of file system operations done with Python
00:24
Along the way, you also learned about what files are, what the file system is, how there’s differences between Windows and Unix systems. You learned about absolute and relative paths, about glob patterns, about recursive matching, and also about the shutil
module.
00:42
Here are some additional resources for you to continue your learning. If you want to learn more about Python 3’s pathlib
module, then you can check out the resources we have on Real Python.
00:52 There’s a written tutorial as well as a video course. And if you want to get more practical working with files in Python, then you can check out Working With Files in Python. Also, here we have a video course as well as a written resource. And of course, there’s the rest of this Python Basics learning path. And in the next course, you will also get more practical working with files.
01:15 Congratulations for making it all the way through this course. Thanks for joining, and hope to see you around Real Python.
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
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?
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.
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?
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.
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?
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?
- Deleting empty and non-empty directories
- 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.
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)
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.
tonypy on Feb. 22, 2023
Thanks for the update and warning.
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.
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.