Search code examples
pythonfilepath

Python - Crossplatform filepath


normally I'm using a mac for my scripts, but now I'm facing a problem on Windows.

Is there a good way to make paths ready for cross platform ?

def load_json(store):
    storename = store
    
    with open('./stores/' + store +'.json' ,'r') as store:
        data = store.read()
    
    object = json.loads(data)

On Windows im getting the error

[Errno 2] No such file or directory: './stores/alternate.json'

Solution

  • Found a way:

    Edited the launch.json File from VS Code on Windows and added:

    "cwd": "${fileDirname}"

    Now VS Code is starting in the right Directory and not in D:\GitHub