From the course: Python Data Science Mistakes to Avoid

Hard coding inaccessible paths - Python Tutorial

From the course: Python Data Science Mistakes to Avoid

Start my 1-month free trial

Hard coding inaccessible paths

- [Instructor] If you hardcode paths that others do not have access to, they cannot run your code, have to search through your code and manually change the paths, and that can be very frustrating and inefficient. To avoid this problem, you can use relative paths. I will be walking through an example to demonstrate how to avoid hardcoding inaccessible paths by using relative paths. Let's say that I'm working with a dataset containing information about AirBnB listings in New York City from 2019. Now let's take a look at where my work is saved. I have a directory named project that contains two sub-directories named code and data. Inside the sub-directory code, I have a Jupiter notebook that contains my code. And inside the sub-directory data, I have a CSV file that contains the dataset I'm working with. Now say I share my project directory with a teammate. When they open the project directory, they'll then be able to open the sub-directory code, after which they can open this Jupiter notebook. When they do, it'll look like this. Let's take a look at this cell here. This is supposed to read the dataset and save it as a pandas data frame. However, the absolute path for the dataset has been hard-coded here. Note that the absolute path for a file is the location of a file relative to the root directory. So the absolute path written here describes where to locate the AirBnB NYC 2019 dataset on my file system and mine alone. That means that when my teammate runs the cell on their system, they will get an error. To avoid this, I can use a relative path instead. Note that the relative path for a file is a location of a file relative to the current working directory. A reminder that I'm currently in the subdirectory code, and note that the parent of the sub-directory code is the project directory. In this new cell, I'll first write down the relative path for the AirBnB NYC 2019 dataset and save it in a variable named Airbnb_path. The dot dot I wrote here signifies the parent of the current directory. In other words, the project directory. And I wrote data here to signify the sub-directory data, which is where the file can be found. And I wrote Airbnb_NYC_2019.csv here, because that's the name of the file. Next, I'll read the dataset using Airbnb_path and save the dataset as a pandas data frame. The relative path written here describes where the dataset can be found relative to the current working directory, which is the directory named code. And my teammate has access to that directory, because I shared it with them. That means that they also have access to this relative path. So when they run the cell, there will be no errors. And when they run this next cell, they'll be able to see the first few rows of the dataset. There you go. Now you know how to use relative paths instead of hardcoding inaccessible paths. Keep this in mind when reading files in your code.

Contents