Skip to content Skip to sidebar Skip to footer

No Such File Or Directory For Relative Path

I have a bottle application in one python file - backend.py. The file contains these definitions: variable = { 'field': [f for f in csv.DictReader(open('../data/fields.csv', 'r

Solution 1:

I think It's better not to rely on working directory. and use path relative to file that is uses this path. I mean you can calculate path on the fly:

import os
csv_path = '../data/fields.csv'
csv_path = os.path.join(os.path.dirname(__file__), csv_path)

In this case one be able to run your script on different environment. an full path will be used to be sure that You don't depends on working directory.

Post a Comment for "No Such File Or Directory For Relative Path"