Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python processing 3 why is current working directory different sometimes?

I am getting this error sometimes when working on mac in Processing for Python. Seemingly for no reason, sometimes the current working directory becomes what you see in the image while other times it is the working directory of the folder the pyde file is in as it should be.

Any ideas on why this is occurring?

Python processing error.

like image 572
Martin Brandel Avatar asked Nov 19 '25 04:11

Martin Brandel


1 Answers

It's to avoid problems like these that I always try to use absolute paths. I would suggest you try something like this for file paths:

import os

# This will be the path to your .py file
FILE_PATH = os.path.dirname(os.path.abspath(__file__))

# This will be the path to your text file, if it is in the same directory as the .py
LEVELS_FILE_PATH = os.path.join(FILE_PATH, "levels.txt")

Then, instead of your current open statement you could have:

f = open(LEVELS_FILE_PATH, 'r')
like image 199
Rolv Apneseth Avatar answered Nov 21 '25 16:11

Rolv Apneseth