python os模块常用路径获取,os.walk路径拼接

版权声明:QQ群:796245415 个人技术交流,禁止用作商业活动 https://blog.csdn.net/chen498858336/article/details/83935008
# coding=utf-8
import os
"""
print os.listdir(os.getcwd())  # get dir all files
print os.path.dirname(__file__),获取文件的父目录路径  
print os.path.abspath(__file__)  # get pwd获取绝对路径文件的.py
print os.path.dirname(os.path.abspath(__file__))   # get dir of pwd 获取绝度路径的父目录
print os.path.realpath(__file__)  # get pwd 获取真实路径同绝对路径
print os.getcwd()  # get current dir path 获取当前目录的父目录路径
路径走读:parent根目录,dirname文件的父目录集合,filenames文件集合
path = os.getcwd()
list1 = []
list2 = []


def os_walk():
    for parent,dirnames,filenames in os.walk(path):
        for dirname in dirnames :
            dir = os.path.join(parent,dirname)
            list1.append(dir)
        for filename in filenames:
            abs = os.path.join(parent, filename)
            list2.append(abs)
    print list1, "\n", list2, "\n", len(list2)
if __name__ == "__main__":
    os_walk()
结果如下:
C:\Python27\python.exe C:/Users/Administrator/PycharmProjects/py3project/dffclassfunc/pathwalk.py
['C:\\Users\\Administrator\\PycharmProjects\\py3project\\dffclassfunc\\dir001'] 
['C:\\Users\\Administrator\\PycharmProjects\\py3project\\dffclassfunc\\clsmth_static_def.py', 'C:\\Users\\Administrator\\PycharmProjects\\py3project\\dffclassfunc\\pathwalk.py', 'C:\\Users\\Administrator\\PycharmProjects\\py3project\\dffclassfunc\\__init__.py', 'C:\\Users\\Administrator\\PycharmProjects\\py3project\\dffclassfunc\\dir001\\__init__.py'] 
4

Process finished with exit code 0

"""

猜你喜欢

转载自blog.csdn.net/chen498858336/article/details/83935008