Relative paths in Python

前端 未结 15 1676
陌清茗
陌清茗 2020-11-22 07:39

I\'m building a simple helper script for work that will copy a couple of template files in our code base to the current directory. I don\'t, however, have the absolute path

15条回答
  •  执笔经年
    2020-11-22 08:11

    This code will return the absolute path to the main script.

    import os
    def whereAmI():
        return os.path.dirname(os.path.realpath(__import__("__main__").__file__))
    

    This will work even in a module.

提交回复
热议问题