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)

相关文章

Django中针对基于类的视图添加csrf_exempt实例代码

在Django中对于基于函数的视图我们可以 @csrf_exempt 注解来标识一个视图可以被跨域访问。那么对于基于类的视图,我们应该怎么办呢? 简单来说可以有两种访问来解决 方法一 在...

Python使用sorted对字典的key或value排序

sorted函数 sorted(iterable,key,reverse) iterable 待排序的可迭代对象 key 对应的是个函数, 该函数用来决定选取用哪些值来进行排...

Win7上搭建Cocos2d-x 3.1.1开发环境

Win7上搭建Cocos2d-x 3.1.1开发环境

开发工具的准备 搭建开发环境需要安装工具包括 Visual Studio python ———(本教程以python2.7.3版本为例),下载地址:http://www.python.o...

Python基于socket实现简单的即时通讯功能示例

本文实例讲述了Python基于socket实现简单的即时通讯功能。分享给大家供大家参考,具体如下: 客户端tcpclient.py # -*- coding: utf-8 -*- i...

Python+django实现文件上传

1、文件上传(input标签)  (1)html代码(form表单用post方法提交) <input class="btn btn-primary col-md-1"...