当前工作路径
os.getcwd()
os.path.abspath(os.curdir)
os.path.abspath('.')
输出结果
C:\Users\jilili\Projects\geogantes\desktop\samples
当前脚本路径
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
print "__file__ = %s" % __file__
print "sys.argv[0] = %s" % sys.argv[0]
print "os.path.realpath(__file__) = %s" % os.path.realpath(__file__)
print "os.path.abspath(__file__) = %s" % os.path.abspath(__file__)
输出结果:
$ python test/test.py
__file__ = test/test.py
sys.argv[0] = test/test.py
os.path.realpath(__file__) = /Users/jilili/Projects/test/test.py
os.path.abspath(__file__) = /Users/jilili/Projects/test/test.py
用户目录
os.path.expanduser('~')
os.path.expandvars('$HOME')
os.environ['HOME']
输出结果
C:\Users\jilili
路径处理
os.path.join可以将多个路径连接成一个完整的路径,但是使用时要注意,如果出现多个以"/"开始的路径将以最后一个为开始路径进行连接
os.path.join("/root", "data", "/mysql")
你知道这个结果是啥吗?那下面这个结果又是啥?
os.path.join("/root", "data", "mysql")
