Learn and live.
12
方式1: os.path.dirname(os.path.dirname(__file__))方式2: os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
dirname是获取输入路径的目录.尽量用方式2,方式1有风险,当你将目录切到当前的脚本所在的目录时并运行该脚本,输出的目录是空.方式2就不会存在这种问题.看下面的例子12345678910111213print("file1 = {}".format(__file__))print("file2 = {}".format(os.path.abspath(__file__)))print("dir1 = {}".format(os.path.dirname(__file__)))print("dir2 = {}".format(os.path.dirname(os.path.dirname(__file__))))print("dir3 = {}".format(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))输出结果:F:\2.2>python _build/translate.pyfile1 = _build/translate.pyfile2 = F:\2.2\_build\translate.pydir1 = _builddir2 =dir3 = F:\2.2
12345678910111213
print("file1 = {}".format(__file__))print("file2 = {}".format(os.path.abspath(__file__)))print("dir1 = {}".format(os.path.dirname(__file__)))print("dir2 = {}".format(os.path.dirname(os.path.dirname(__file__))))print("dir3 = {}".format(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))输出结果:F:\2.2>python _build/translate.pyfile1 = _build/translate.pyfile2 = F:\2.2\_build\translate.pydir1 = _builddir2 =dir3 = F:\2.2
都指某个进程运行时所在的目录.pwd 是linux 自带的命令.全称:pathname of the current working directory.$PWD 是个系统变量cwd: 不是系统自带的命令, 但是属于系统的属性.全称: current working directory . 不但在 /proc/{id} 这个目录下可以看到cwd, 在很多其他的编程语言中也可以看到( 例如grunt ).有时候 pwd 与 $PWD 给出的结果不同.