python使用os模块的os.walk遍历文件夹示例

yipeiwu_com5年前Python基础

复制代码 代码如下:

#-*- coding:utf-8 -*-

import os

if __name__ == '__main__':
    try:
    '''traval and list all files and all dirs'''
    for root, dirs, files in os.walk('D:' + os.sep + 'Python27'):
        print '-------------------directory < ' + root + ' > --------------------------'

        for d in dirs:
        print d
        for f in files:
        print f
    except OSError, e:
    print os.strerror(e.errno)

相关文章

深入了解Python枚举类型的相关知识

枚举类型可以看作是一种标签或是一系列常量的集合,通常用于表示某些特定的有限集合,例如星期、月份、状态等。 Python 的原生类型(Built-in types)里并没有专门的枚举类型,...

python自定义类并使用的方法

本文实例讲述了python自定义类并使用的方法。分享给大家供大家参考。具体如下: class Person: def __init__(self, first, middle,...

Python中字符串格式化str.format的详细介绍

前言 Python 在 2.6 版本中新加了一个字符串格式化方法: str.format() 。它的基本语法是通过 {} 和 : 来代替以前的 %.。 格式化时的占位符语法: rep...

python数据类型_字符串常用操作(详解)

这次主要介绍字符串常用操作方法及例子 1.python字符串 在python中声明一个字符串,通常有三种方法:在它的两边加上单引号、双引号或者三引号,如下: name = 'hell...

python的dataframe和matrix的互换方法

实例如下所示: #-*- encoding:utf-8 -*- import pandas as pd import numpy as np df = pd.DataFrame(np...