0, os.path.realpath(file)) 真实文件路径

  1. def TestPrtPwd(self):
  2. print("获取当前文件路径——" + os.path.realpath(__file__)) # 获取当前文件路径
  3. parent = os.path.dirname(os.path.realpath(__file__))
  4. print("获取其父目录——" + parent) # 从当前文件路径中获取目录
  5. garder = os.path.dirname(parent)
  6. print("获取父目录的父目录——" + garder)
  7. print("获取文件名" + os.path.basename(os.path.realpath(__file__))) # 获取文件名
  8. # 当前文件的路径
  9. pwd = os.getcwd()
  10. print("当前运行文件路径" + pwd)
  11. # 当前文件的父路径
  12. father_path = os.path.abspath(os.path.dirname(pwd) + os.path.sep + ".")
  13. print("运行文件父路径" + father_path)
  14. # 当前文件的前两级目录
  15. grader_father = os.path.abspath(os.path.dirname(pwd) + os.path.sep + "..")
  16. print("运行文件父路径的父路径" + grader_father)
  17. return garder

1、sys.argv—- sys.argv[0]是“执行”脚本的名字

sys.argv保存的是脚本的指令参数列表,sys.argv[0]是“执行”脚本的名字。print(sys.argv[0])返回相对路径还是绝对路径,主要看你脚本的执行方式,如下:

  1. #当前路径
  2. (base) SchillerdeMacBook-Pro:numpy_test schillerxu$ pwd
  3. /Users/schillerxu/Documents/sourcecode/python/numpy_test
  4. #代码
  5. (base) SchillerdeMacBook-Pro:numpy_test schillerxu$ cat 3.py
  6. import sys
  7. import os
  8. print(sys.argv)
  9. print(sys.argv[0])
  10. #两种不同的执行方法对比
  11. (base) SchillerdeMacBook-Pro:numpy_test schillerxu$ python 3.py
  12. ['3.py']
  13. 3.py
  14. (base) SchillerdeMacBook-Pro:numpy_test schillerxu$ python /Users/schillerxu/Documents/sourcecode/python/numpy_test/3.py
  15. ['/Users/schillerxu/Documents/sourcecode/python/numpy_test/3.py']
  16. /Users/schillerxu/Documents/sourcecode/python/numpy_test/3.py

如果获取的是相对路径,可以用os.path.abspath(sys.argv[0])得到绝对路径,用os.path.split()可以得到目录名和文件名,代码如下:

  1. import sys
  2. import os
  3. path=sys.argv[0]
  4. abs_path=os.path.abspath(sys.argv[0])
  5. dirname,filename=os.path.split(abs_path)
  6. print(path)
  7. print(abs_path)
  8. print(dirname,filename)

程序运行的结果如下:

  1. (base) SchillerdeMacBook-Pro:numpy_test schillerxu$ pwd
  2. /Users/schillerxu/Documents/sourcecode/python/numpy_test
  3. (base) SchillerdeMacBook-Pro:numpy_test schillerxu$ python 3.py
  4. 3.py
  5. /Users/schillerxu/Documents/sourcecode/python/numpy_test/3.py
  6. /Users/schillerxu/Documents/sourcecode/python/numpy_test 3.py

最后获得目录名和文件名。

2、 file

file是当前脚本的名字,如果在当前脚本使用, file和sys.argv[0]效果是一样的,比如代码如下:

  1. import sys
  2. import os
  3. print(__file__)

运行结果:

  1. (base) SchillerdeMacBook-Pro:numpy_test schillerxu$ python 3.py
  2. 3.py
  3. (base) SchillerdeMacBook-Pro:numpy_test schillerxu$ python /Users/schillerxu/Documents/sourcecode/python/numpy_test/3.py
  4. /Users/schillerxu/Documents/sourcecode/python/numpy_test/3.py

同样受到脚本执行参数的影响。

但是如果脚本互相引用, file和sys.argv[0]结果略有不同,比如代码:

  1. # a.py
  2. import sys
  3. def f():
  4. print(sys.argv[0])
  5. print(__file__)
  1. ## 3.py
  2. import sys
  3. import os
  4. import a
  5. print(sys.argv[0])
  6. print(__file__)
  7. a.f()

执行结果:

目录操作 - 图1
可以看到file返回的是被调用(当前)的文件,sys.argv[0]更多的是脚本执行的参数。

3、sys.path

sys.path保存了python解释器的部分路径,可以自己试下,其中sys.path[0]是执行脚本所在的目录。
代码:

  1. import sys
  2. import os
  3. print(sys.path[0])

结果:

  1. $ python 3.py
  2. /Users/schillerxu/Documents/sourcecode/python/numpy_test

4、os.getcwd()—工作目录

os.getcwd()返回的是工作目录,并不一定是脚本所在目录。

代码:

  1. import sys
  2. import os
  3. print(os.getcwd())

运行结果:
目录操作 - 图2