File Locations and Paths
When you access a file on an operating system, a file path is required. The file path is a string that represents the location of a file. It’s broken up into three major parts:
- Folder Path: the file folder location on the file system where subsequent folders are separated by a forward slash
/
(Unix) or backslash\
(Windows) - File Name: the actual name of the file
- Extension: the end of the file path pre-pended with a period (
.
) used to indicate the file type
00:00 File locations and paths.
00:05 So far, we’ve only been working with files which are in the same location as our script, but generally we would want to do something more complicated than that. Files can be located with file paths, a string that represents the location of a file.
00:19 They can be broken up into three major parts: the path where the file is located, which we’ll be looking at in the next section; the filename, the name of the file in question; and the extension. A path gives the location on the file system where a file is located, and they can be relative or absolute.
00:45
Relative paths on Windows. So, relative paths start in the current directory and start with either a directory or a filename. The first one is simple, just a filename, my_file.py
.
00:57
The second one is a folder, or directory, data\
, with info.txt
in. The third one follows a similar pattern, but with directories of config\users\darren\
and then settings.json
being the file.
01:10
The fourth one has the double dot character (..
), which takes you up a level, then you descend into \peer\folder\data\
to find config.txt
. Note that the path is separated with the backslash character. This applies only to Windows.
01:26 Relative paths on other OSes follow the same pattern, with one exception. The path is separated with a forward slash character rather than backslash. This is something that we need to deal with. Fortunately, Python gives us a solution. We’ll look at the module that allows us to do this after we’ve seen what absolute paths look like.
01:47
Absolute paths give the full explicit location of a file or folder, a bit like the difference between saying “John lives up the road from me,” or giving his full address. On Windows, a full path will begin with a drive letter, such as C
. On macOS and Linux, a full path will consist of only folder names, as they do not have drive letters.
02:12
To deal with these different separators, the os
module provides a solution. os.path.join()
creates paths that are appropriate for the OS that you’re running on. Here you can see some code where the os
module is imported and then folder_path
is created using os.path.join()
with the values of "Users"
, "Fred"
, and "Downloads"
. Finally, the folder_path
is printed, and as you can see below, there’s a different result on Windows than macOS.
02:44 Windows has given backslashes, macOS has given forward slashes, as it would be if you’re running on Linux as well. But you don’t need to take my word for it.
02:54
You can see it in action. Here we are in VS Code on Windows with the code you’ve just seen, where the folder_path
has been made using os.path.join()
. Running that script gives us a result with the arguments being separated by backslashes.
03:14
Now you can see exactly the same code with os.path.join()
joining together 'Users'
, 'Fred'
, and 'Downloads'
being run on a Mac.
03:24 As you’ll see, identical code has given us a different result, putting the right separators in for the OS in question.
03:34
The filename and extension are often thought of as a single element, but actually shopping_list
is the filename and .txt
is the extension.
03:45
os.sep
allows creation of the appropriate separator for absolute file paths. Using it as the first argument in our os.path.join()
here allows that creation of that forward slash at the beginning of the result, which creates an absolute file path on macOS or Linux.
04:07
Using exactly the same code on Windows produces a nearly absolute file path, depending on your point of view. It’s absolute for the disk that you’re on and it’s created with backslashes, but Windows systems can have multiple disks, which are referenced using C
, D
, et cetera—drive letters.
04:28
In those cases, it is possible to create a truly absolute file path, as you can see here by the addition of the drive letter at the beginning, although whether this will be the right solution for you is something you’ll have to consider yourselves. Here, you can see an example of using a file path which has been created using os.path.join()
and the file_path
variable has been passed to open()
rather than an explicit filename as we’ve seen in all the other examples. Other than that, this is all code that you’ve seen before, and it works in exactly the same way.
Become a Member to join the conversation.