Search code examples
pythonprocessing

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.


Solution

  • 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')