The File System
00:00 In the previous lesson, you learned what a file really is, that it is a sequence of bits stuck together basically, and that it depends on the program and your computer and how to interpret these bits.
00:11
Now you need a way of handling all these bits that are saved on your storage. So if you want to revisit the file content that you saw in the previous lesson, this is the content of a text file that says Hello, World
with an !
at the end. And it looks like this in bits, but now imagine this being stored somewhere on your storage disk, and then after it, maybe you have a PNG file.
00:35 And this is just the start of an image file, but it just looks the same, you know, there’s bits that are on and off and they’re just saved like this on the disk.
00:43 Now how does your operating system know that from here to here is where the text file is and after it comes something else, you know? This task that is a difficult task to handle is done by the file system on your computer. The file system essentially does two things: provides an abstract representation of files that are stored on the computer, which you can imagine as being some sort of a dictionary lookup where you have an abstract information of which bits you want to address, and then the file system redirects to those places in memory and gives them back to you to basically assemble the file, and then it also controls the storage and retrieval of that file data, so it interfaces with the hardware to then actually return these different pieces that can be spread out across the the storage disk.
01:33 So this is a whole complicated process. The good thing is you don’t really have to worry about it because this is something that the file system handles for you, and Python just interacts with the file system and utilizes the functional data that’s built-in there.
01:47 One thing to remember is that different operating systems use different file systems. There’s loads of different file systems out there. So as you might see, you can run into a problem here that if you want to write Python code that works the same on different operating systems and interacts with files, that you could run into troubles there if this is handled differently.
02:09 But Python has a good way around it, and you’ll learn more about this coming up in just a bit. Now let’s think a little more about the file system. It’s structured in a hierarchy. You can think of it as directories and files.
02:23 So directories are containers for other directories or files. So in the end, you could think that there’s a root directory, which is the the first structure that holds all the other information, and then you can have multiple directories nested in there. You can have files, you can have subdirectories, and even more files.
02:42 And this is what’s called the directory tree. And now I’m going to show you a picture of the directory tree. Beautiful, right? Of course, this is not actually the directory tree. It looks more like this, maybe like an upside-down tree, where you have a root folder that sits at the very beginning, then you have some other directories nested in there, And these directories can contain files, or they con contain other directories, which again contain files.
03:11 This is an abstract representation of what’s going on on the storage device. Remember, it’s all bits all the way down, basically, but you need some way of conceptualizing this and addressing these bits.
03:24
And the file system gives you this option that if you wanted to, for example, find this reishi.gif
, then you could address it like this. You would give it a file path that starts from the beginning, from the root/
, then goes into the next directory photos/fungi/
, and then finally the name of the file.
03:43 And this gives you the location, and the file system translates this path to the actual location of the data that is saved on your search device so that you can access this image.
03:56
Now you just learned that different operating systems use different file systems, and as a consequence, also the file paths are different on different operating systems. So if you look at a file path for macOS, it can look something like that where the first /
(forward slash) represents the route directory, then there’s a subdirectory called Users/
, then this is my username, my user directory, then I go into the Documents/
folder, and finally there is a .txt
file called hello.txt
.
04:25
On Ubuntu Linux, this would look slightly different. You’d start again with a root directory, but then instead of Users/
, you have a directory called home/
that contains the different user directories, and then again, Documents/
and hello.txt
. Windows is quite different to those two. The same type of file path would look like this, where you start off with a C:
then a \
(backslash), then a directory called Users\
, then the username, Documents\
, and filename.
04:55
And the main difference you’ll notice here is that you start off with C:
instead of just a slash, as well as that all the slashes are backslashes instead of forward slashes.
05:05
So these look quite different. You may think that that makes it hard for you to write some code that finds a file in the Documents
folder, for example. But lucky for you, working with file paths in Python is relatively straightforward because of the build-in pathlib
module. And you’ll learn more about how to use it in the next lesson.
Become a Member to join the conversation.