dojogeorge
dojogeorge

Reputation: 1704

IOError: [Errno 2] No such file or directory: 'users.txt'

I am getting the above error when I use a webserver to run my code, however locally in the Terminal this works fine. I believe this must be to do with the path to the file working locally but not remotely. I have seen the solution on stackoverflow is to add the filepath like '/user/xxx/library/' etc, however is there a solution that allows this to be system agnostic? As in if I copy this directory to another location/server it will still work?

Upvotes: 0

Views: 1585

Answers (1)

user1301404
user1301404

Reputation:

You can import os, it's built it to Python. You can get teh absolute path of the .py file this way:

import os
ROOT = lambda base : os.path.join(os.path.dirname(__file__), base).replace('\\','/')

Now you can simply do the following:

ROOT('users.txt') 

It should return the absolute path.

Upvotes: 1

Related Questions