Python: get path to file in sister directory?

前端 未结 4 1526
小鲜肉
小鲜肉 2021-01-03 00:06

I have a file structure like this:

data
   mydata.xls
scripts
   myscript.py

From within myscript.py, how can I get the filepath of mydata.

4条回答
  •  耶瑟儿~
    2021-01-03 00:23

    import os
    directory = os.path.dirname(os.getcwd())
    final = os.path.join(directory, 'data', 'mydata.xls')
    

    or simply

    os.path.abspath('../data/mydata.xls')
    

提交回复
热议问题