user9108529
user9108529

Reputation:

Csv File Not Found in Django

I am probably missing something obvious but my python code does not recognise my csv file although it is included in my package. As a result, I am getting a FileNotFoundException. Here is my error:

 from multiplication import views
 File "/Users/NikolasPapastavrou/firstProject/multiplication/views.py", 
 line 15, in <module>
 with open('data.csv','r') as csv_file:
 FileNotFoundError: [Errno 2] No such file or directory: 'data.csv'

Here is a picture of my setup, including the file in the package: https://i.sstatic.net/l1pW7.png

Upvotes: 1

Views: 1397

Answers (1)

Haifeng Zhang
Haifeng Zhang

Reputation: 31885

When you start a Django by python manage.py runserver, the current working path is where your manage.py locates. If you put your data.csv under the same folder, it would work. Alternatively, provide the absolute path to your data file:

DATA_SRC = '/Users/NikolasPapastavrou/firstProject/multiplication/data.csv'

with open(DATA_SRC, 'r') as csv_file:
    // do your work...

Upvotes: 5

Related Questions