文件绝对路径,相对路径

工程文件结构如下

|----subdir

|         |-----subpath.py (在这个文件中获取当前文件夹路径)

|----mainpath.py

 

如果想在subpath.py中 获取当前文件夹路径: /root/subdir/

可以使用os.getcwd()

但是如果程序从mainpath中执行,调用subpath.py,那么os.getcwd()获取的就是/root/

希望不管是从mainpath.py启动,还是从subpath.py中启动,都能返回/root/subdir

使用绝对路径:

curfile = os.path.abspath(__file__)
curpath = os.path.dirname(curfile)

#或者

curfile = os.path.abspath(__file__)
curpath = os.path.abspath(os.path.dirname(curfile) + os.path.sep + ".")

 

posted @ 2019-04-01 14:50  桃乐丝  阅读(296)  评论(0编辑  收藏  举报