#coding=utf-8

import os
def find_file(arg,dirname,files):
    #for i in arg:
        #print i

for file in files:
        file_path=os.path.join(dirname,file)
        print 'file_path:',file_path
        if os.path.isfile(file_path) and (arg[0] in file or arg[1] in file):
            print 'file:%s\n'%file_path

os.path.walk(r'd:\\test2',find_file,('.txt','.png'))

c:\Python27\Scripts>python task_test.py
file_path: d:\\test2\file1
file_path: d:\\test2\file1.txt
file:d:\\test2\file1.txt

file_path: d:\\test2\file2
file_path: d:\\test2\file1\file2.txt
file:d:\\test2\file1\file2.txt

file_path: d:\\test2\file2\file3
file_path: d:\\test2\file2\file3.txt
file:d:\\test2\file2\file3.txt

file_path: d:\\test2\file2\file3\file4.txt
file:d:\\test2\file2\file3\file4.txt

进一步看每次循环dirname,file都是什么:

#coding=utf-8

import os
def find_file(arg,dirname,files):
    #for i in arg:
        #print i

for file in files:
        print "dirname: %s, file:%s" %(dirname,file)
        file_path=os.path.join(dirname,file)
        print 'file_path:',file_path
        if os.path.isfile(file_path) and (arg[0] in file or arg[1] in file):
            print 'file:%s\n'%file_path

os.path.walk(r'd:\\test2',find_file,('.txt','.png'))

c:\Python27\Scripts>python task_test.py
dirname: d:\\test2, file:file1
file_path: d:\\test2\file1
dirname: d:\\test2, file:file1.txt
file_path: d:\\test2\file1.txt
file:d:\\test2\file1.txt

dirname: d:\\test2, file:file2
file_path: d:\\test2\file2
dirname: d:\\test2\file1, file:file2.txt
file_path: d:\\test2\file1\file2.txt
file:d:\\test2\file1\file2.txt

dirname: d:\\test2\file2, file:file3
file_path: d:\\test2\file2\file3
dirname: d:\\test2\file2, file:file3.txt
file_path: d:\\test2\file2\file3.txt
file:d:\\test2\file2\file3.txt

dirname: d:\\test2\file2\file3, file:file4.txt
file_path: d:\\test2\file2\file3\file4.txt
file:d:\\test2\file2\file3\file4.txt

函数解释:

os.path.walk(top, func, arg)

回调函数(钩子),当一个事件发生时,自动调用指定函数

参数说明:

top:表示需要遍历的目录树的路径

func:表示回调函数,对遍历路径进行处理的函数。所谓回调函数,是作为某个函数的的参数使用,当某个时间触发时,程序将调用定义好的回调函数处理某个任务。该回调函数必须提供3个参数:第1个参数为walk()的参数arg,第2个参数表示目录列表dirname,第3个参数表示文件列表names。

arg:是传递给回调函数func的元组,为回调函数提供处理参数,回调函数的第一个参数就是用来接收这个传入的元组的,参数arg可以为空)

#coding=utf-8

import os

#回调函数

#coding=utf-8

import os

#回调函数

#调find_file函数时(1,2)传给arg,dirname和files是walk函数来传递的

def find_file(arg, dirname, files):

#for i in arg:

#    print i

for file in files:

file_path = os.path.join(dirname, file)

if os.path.isfile(file_path):

print "file:%s" %file_path

#调用

os.path.walk(r"d:\test2", find_file, (1,2))

python 文件操作,os.path.walk()的回调函数打印文件名的更多相关文章

  1. 关于python 文件操作os.fdopen(), os.close(), tempfile.mkstemp()

    嗯.最近在弄的东西也跟这个有关系,由于c基础渣渣.现在基本上都忘记得差不多的情况下,是需要花点功夫才能弄明白. 每个语言都有相关的文件操作. 今天在flask 的例子里看到这样一句话.拉开了文件操作折 ...

  2. Python基本知识 os.path.join与split() 函数

    Python中有join和os.path.join()两个函数,具体作用如下: join:连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符)连接生成一个新的字符串os.path.joi ...

  3. python文件操作os模块

    Python 统计某一文件夹下文件数量 使用python  pathlib模块 from pathlib import Path dir_path = ' ' print(len(list(Path( ...

  4. python os.walk()和os.path.walk()

    一.os.walk() 函数声明:os.walk(top,topdown=True,onerror=None) (1)参数top表示需要遍历的顶级目录的路径. (2)参数topdown的默认值是“Tr ...

  5. Python:os.walk()和os.path.walk()用法

    转于:https://www.cnblogs.com/zmlctt/p/4222621.html 博主:zmlctt 一.os.walk() 函数声明:os.walk(top,topdown=True ...

  6. python(3):文件操作/os库

      文件基本操作 r,以读模式打开,  r+=r+w, w, 写模式(清空原来的内容), w+=w+r, a , 追加模式, a+=a+r, rb, wb, ab, b表示以二进制文件打开 想在一段文 ...

  7. day18 时间:time:,日历:calendar,可以运算的时间:datatime,系统:sys, 操作系统:os,系统路径操作:os.path,跨文件夹移动文件,递归删除的思路,递归遍历打印目标路径中所有的txt文件,项目开发周期

    复习 ''' 1.跨文件夹导包 - 不用考虑包的情况下直接导入文件夹(包)下的具体模块 2.__name__: py自执行 '__main__' | py被导入执行 '模块名' 3.包:一系列模块的集 ...

  8. python 简单示例说明os.walk和os.path.walk的不同

    import os,os.path def func(arg,dirname,names): for filespath in names: print os.path.join(dirname,fi ...

  9. python文件操作及os模块常用命令

    1.文件打开 文件句柄 = open('文件路径', '模式') 2.文件操作 打开文件时,需要指定文件路径和以何等方式打开文件,打开后,即可获取该文件句柄,日后通过此文件句柄对该文件操作. 三种基本 ...

随机推荐

  1. IntelliJ IDEA导出Java 可执行Jar包

    extends:http://blog.sina.com.cn/s/blog_3fe961ae0102uy42.html 保证自己的Java代码是没有问题的,在IDEA里面是可以正常运行的,然后,按下 ...

  2. nginx虚拟目录配置

    参考文章:https://blog.csdn.net/whatday/article/details/50649461 1. location ~ ^/awstats/ { root /home/aw ...

  3. spring task的定时任务突然断了

    spring定时任务只开启一个线程去工作也就是串行工作,定时调度任务出现阻塞导致线程终止 加上这个试试 <!-- <task:annotation-driven /> --> ...

  4. 【转载】纵观RTX51

    对于使用RTX51的具体好处可以在实践中去体会,就象会用了C51,就不想再用汇编了.用了RTX51,说不定就感到再也离不开它了. 1.RTX51是实时多任务操作系统RTX51是一种实时操作系统既目前在 ...

  5. Git 使用篇一:初步使用GitHub,下载安装git,并上传项目

    首先在MAC上怎么操作. 在gitHub创立一个账户,在创立一个项目,这就不用我说了对吧. 创建完之后是这样的: 接下来,我们打开https://brew.sh 这是下载homebrew的网站,hom ...

  6. HUSTM 1601 - Shepherd

    题目描述 Hehe keeps a flock of sheep, numbered from 1 to n and each with a weight wi. To keep the sheep ...

  7. hdfs启用垃圾站功能

    在core-site.xml文件中添加如下内容: ##开启回收站功能,设置保存7天删除数据信息        <property>                <name>f ...

  8. First normal formal Second normal form

    https://en.wikipedia.org/wiki/First_normal_form https://en.wikipedia.org/wiki/Second_normal_form A r ...

  9. Vue源码学习1——Vue构造函数

    Vue源码学习1--Vue构造函数 这是我第一次正式阅读大型框架源码,刚开始的时候完全不知道该如何入手.Vue源码clone下来之后这么多文件夹,Vue的这么多方法和概念都在哪,完全没有头绪.现在也只 ...

  10. BigDecimal精度与相等比较的坑

    先想一下,创建BigDecimal对象的时候一般是怎么创建的? new一个,传进去值 BigDecimal.valueOf方法,传进去值 作为一个数字类型,经常有的操作是比较大小,有一种情况是比较是否 ...