Skip to content
Advertisement

When import a python file into another (in a subdirectory) it stops finding the csv in the same directory

I’m trying to import the cleaning file in the managers file like this:

JavaScript

The main folder contains the files cleaning, transaction listing and a sub folder called “apps” with the managers file:

enter image description here

Also note that the cleaning file reads the transaction listing.csv file.

The issue I’m getting when running is:

JavaScript

And “Transaction Listings.csv” is correctly loaded by cleaning but no when cleaning is imported into maganagers.

Appreciate your help.

Advertisement

Answer

JavaScript

Use this code in cleaning.py instead of your current code.

What is happening is that when cleaning.py is imported into apps/managers.py, it runs in the apps folder, not the base folder where the CSV files are located. By giving the absolute path, pd.read_csv() will look in the correct place for the file – the directory that cleaning.py actually resides in. See what does the __file__ variable mean/do?.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement