文件绝对路径,相对路径

工程文件结构如下

|----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 + ".")
原文地址:https://www.cnblogs.com/taurusfy/p/10636385.html