python----获取文件的路径(各种方式)

#获取到当前文件的上个文件夹目录
ABSPATH = os.path.abspath(os.path.realpath(os.path.dirname(__file__)))

#获取的是相对路径
FRONTEND = os.path.abspath(os.path.join(ABSPATH, sys.argv[0]))
print ABSPATH
print FRONTEND

#获取的是相对路径
print sys.argv[0]


结果:

/Users/lucky/PycharmProjects/lucky_L/test_02
/Users/lucky/PycharmProjects/lucky_L/test_02/test.py
/Users/lucky/PycharmProjects/lucky_L/test_02/test.py

猜你喜欢

转载自www.cnblogs.com/syw20170419/p/11002963.html
今日推荐