How do I get the full path of the current file's directory?

前端 未结 15 1152
傲寒
傲寒 2020-11-22 17:01

I want to get the current file\'s directory path. I tried:

>>> os.path.abspath(__file__)
\'C:\\\\python27\\\\test.py\'

But how can

15条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 17:33

    You can use os and os.path library easily as follows

    import os
    os.chdir(os.path.dirname(os.getcwd()))
    

    os.path.dirname returns upper directory from current one. It lets us change to an upper level without passing any file argument and without knowing absolute path.

提交回复
热议问题