dogover

1、获取当前路径

current_path1 = os.path.abspath(__file__)      

current_path2 = os.path.realpath(__file__)

两种方式返回结果一样:
D:\PythonProject\test\app01\config\read_yaml.py

D:\PythonProject\test\app01\config\read_yaml.py

 

2、获取父路径

pra_path1 = os.path.abspath(os.curdir)

pra_path2 = os.path.dirname(os.path.abspath(__file__))

放回结果:

D:\PythonProject\test\app01\config

D:\PythonProject\test\app01\config

区别:

pra_path1返回的是执行文件所在文件夹,如果其他文件调用read_yaml.py,则返回其他文件的父路径。

例如文件D:\PythonProject\test\app01\common\cc.py调用read_yaml.py,则返回D:\PythonProject\test\app01\common

pra_path2返回的是read_yaml.py文件所在文件夹,不管谁调用返回均为D:\PythonProject\test\app01\config

 

3、获取父路径的父路径

pra_path3 = os.path.dirname(pra_path2)

 

4、路径拼接

new_path = os.path.join(pra_path2,‘report’,\'config.ini\'

 

5、创建路径

 if not os.path.exists(new_path):

  os.makedirs(new_path)

 

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-02-06
  • 2022-02-10
  • 2021-07-27
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-04
  • 2022-12-23
  • 2022-12-23
  • 2021-12-10
相关资源
相似解决方案