zoukankan      html  css  js  c++  java
  • python3.4下遍历文件目录,不需要再特殊处理中文编码

    python3.4下遍历文件目录,不需要再特殊处理中文编码

    直接使用os.walk来遍历中文目录。

    os.walk方法返回的是一个三元

    tupple(dirpath, dirnames, filenames),

    其中第一个为起始路径,

    第二个为起始路径下的文件夹,

    第三个是起始路径下的文件.

    dirpath是一个string,代表目录的路径,

    dirnames是一个list,包含了dirpath下所有子目录的名字,

    filenames是一个list,包含了非目录文件的名字.这些名字不包含路径信息,如果需要得到全路径,需要使用 os.path.join(dirpath, name).

    以上内容就是对Python os.walk 遍历目录的实际相关介绍。

    __author__ = 'Administrator'
    import os
    
    print(os.sep)
    path = "d:工作目录"+os.sep
    print(path)
    print(os.walk(path))
    for root,dirs,files in os.walk(path):
        print(root)
        for file in files:
            if root ==path:
                print(root+file)
            else:
                print(root+os.sep+file)
        for dirName in dirs:
            print(root+dirName)

    通过对root路径的遍历,实现对所有文件的访问

  • 相关阅读:
    MaaS系统概述
    流处理认识
    事务补偿
    Hystrix原理与实战
    RPC概念和框架
    git remote: error: hook declined to update
    Unity CombineTexture
    Windows Powershell统计代码行数
    unity面试题二
    unity面试题一
  • 原文地址:https://www.cnblogs.com/liguangao/p/5170473.html
Copyright © 2011-2022 走看看