Python 获取文件路径

demo_dir.py

import os
 
"""获取当前目录"""
print(os.getcwd())
print(os.path.abspath(os.path.dirname(__file__)))
"""获取上一级目录"""
print(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
print(os.path.abspath(os.path.dirname(os.getcwd())))
print(os.path.abspath(os.path.join(os.getcwd(), "..")))
"""获取上上级目录"""
print(os.path.abspath(os.path.join(os.getcwd(), "../..")))
 
 
"""获取当前路径的文件夹以及路径"""
 
all_file = [f for f in os.listdir(os.getcwd())]
for all_ in all_file:
    if os.path.isdir(all_):
        print(all_)

 

posted @ 2022-10-26 10:19  QYGQH  阅读(122)  评论(0编辑  收藏  举报