Loading video player…

Python Basics: File System Operations (Overview)

So far, you’ve written programs that get their input from one of two sources: the user or the program itself. Program output has been limited to displaying some text in IDLE’s interactive window.

These input and output strategies aren’t useful in several common scenarios, such as when:

  • The input values are unknown while writing the program.
  • The program requires more data than a user can be expected to type in by themselves.
  • Output must be shared with others after the program runs.

This is where files come in. You’ve likely been working with computer files for a long time. Even so, there are some things that programmers need to know about files that general users do not. Specifically, you’ll want to master some file system operations using the pathlib and shutil modules.

In this video course, you’ll learn 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

This video course is part of the Python Basics series, which accompanies Python Basics: A Practical Introduction to Python 3. You can also check out the other Python Basics courses.

Note that you’ll be using IDLE to interact with Python throughout this course. If you’re just getting started, then you might want to check out Python Basics: Setting Up Python before diving into this course.

Download

Sample Code (.zip)

22.5 KB
Download

Course Slides (.pdf)

6.0 MB

00:00 Welcome to this Python Basics course on file system operations. My name is Martin, and I’m going to walk you through the course. Now, what are file system operations? Basically, it’s the things that you do in a daily basis on your computer.

00:14 It means creating files and folders, looking at the contents of a folder, searching for files and folders, and also moving and deleting files and folders.

00:23 Those are all file system operations. You usually do them with your operating system, but you can also do them with Python. And in this course, you will learn how to do all these operations using Python’s pathlib module. But before we go there, just for a second, why would you even want to use files and folders when you have something like the input() function and also then terminal output?

00:46 Why do you need files for programming? Now, I have three reasons for you. First is persistence. If you just run your program and you don’t store the data anywhere in the file, your output is going to get lost when the program terminates.

01:00 Second is convenience. If you need to input all the data that you want to use in your program, which might be a large amount, just by hand, then this is going to take you much longer than if you just load it from a file.

01:12 And third is mobility. You might want to move your data from your computer to another computer, and this works really well with using files.

01:22 Like I mentioned before, in this course, you will 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 move and delete files and folders.

01:35 Note that in this course I will use the words folder and directory interchangeably. They’ll stand for the same thing.

01:44 Along the way, you will also learn about what files are, what the file system is, about some differences between Windows and UNIX systems, about absolute and relative paths, about glob patterns for searching, about recursive matching, and finally also the shutil module.

02:04 Here’s the table of contents. You’re starting off with this overview lesson. That’s the one you’re in right now. Next, you will learn about the anatomy of file and then about the file system.

02:14 And then you already start getting a bit more practical. You’ll learn how to create Path objects, how to check whether a file path exists. You’ll learn how to identify absolute and relative paths, how to resolve relative paths, how to access file path components.

02:31 You’ll also learn how to create directories and subdirectories, how to create files, how to iterate over directory contents, how to search for files using .glob(), and then you’ll also understand some common wildcard characters—such as the * (star), the ? (question mark), and the [] (square brackets)—that you can use with .glob(). You’ll learn how to search recursively, move files and directories, and finally, also how to delete files and directories.

02:56 And then the last lesson is going to be a summary, and I’ll also point you forward to some additional resources that you can use to further your learning. Throughout the course, you will be using IDLE which stands for Integrated Development and Learning Environment, and it comes with your Python installation.

03:14 If you’ve gone through this whole series of Python Basics courses, then you should be set up and running with IDLE. Otherwise, you can go ahead and check out one of these two resources on Real Python to learn more about how to use IDLE. And with that, let’s get started on the Python Basics: File System Operations course by learning a little more about files.

Become a Member to join the conversation.