How to Programming with Directories in Python
Just as we have seen how to work with files, Python provides different functions that allow us to create, delete and navigate the contents of directories.
Get the path of a directory
To check what current directory your Python program is running in we use the .getcwd() method:
In this case, the current directory is our user’s home directory, i.e. the default directory we start with when opening on macOS.
Create a new directory
To create a directory we use the mkdir() method and provide it with a name as a String. This directory will become the current working directory:
Change the current working directory to another directory
We can also change the current working directory using the .chdir() method passing the directory we want to change to as a parameter (in this case, we can use both absolute and relative paths).
We can see how we have changed the current working directory to a subdirectory.
Delete a directory
We use the .rmdir() function to remove directories. We proceed to create a new directory and then delete it. It is important to note that this will only work if the directory is empty.
In the following example, we try to delete the new_directory directory, which is not empty, it contains the data.txt file, we see how it throws us an error:
We have previously created a directory called new_directory_2 (which is empty), and we proceed to delete it:
Get the contents of a directory
To obtain the content of a given directory we use the .listdir() method, which returns a list of all the files and subdirectories found in a given directory:
In this case, it tells us a list of Strings, but we don’t know exactly if they are directories or files. To find out if any of these Strings is a directory, we need to use os.path.join to create the full path and then use the .isdir() function to see if it is a directory.
We will do the following:
- We define a variable ‘dir’ with the name of the directory we want to check (in order to make our code more readable and reusable).
2. We iterate through the filenames returned by the os.listdir. Until this moment, we know that we only have the names of the files without a directory. So using the os.path.join , we join the directory to each of those filenames and create a string with a valid full name.
3. Finally, we use that fully qualified name to call os.path.isdir to check if it’s a directory or a file. The ‘join’ function allows us to be independent of the operating system.
On Linux and MacOS, parts of a file are split using a forward slash (/), on Windows, they are split using a backslash (\), using the os.path.join function instead of explicitly adding a slash diagonal, we can ensure that our scripts work with all operating systems. This allows us to avoid errors when working on different platforms.
I hope this article has helped you understand working with directories in Python. Please support me by ‘clapping’ and following me so I can continue contributing to the community. Thank you!
Estefanía