open() can't find file given path relative to PYTHONPATH

后端 未结 3 844
暖寄归人
暖寄归人 2021-01-16 07:38

I did export PYTHONPATH=$PYTHONPATH:/home/User/folder/test. Then I ran python when I was in /home/User/ and checked sys.path - it was

3条回答
  •  余生分开走
    2021-01-16 08:04

    If I'm reading your question correctly, you want your data to reside in a location relative to the module. If that's the case, you can use:

    full_path = os.path.join(os.path.split(__file__)[:-1]+['pics','text','text.txt'])
    

    __file__ is the path to the module (including modulename.py). So I split that path, pull off modulename.py ([:-1]) and add the rest of the relative path via os.path.join

提交回复
热议问题