#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. 7.20python线程(2)

    RLock 递归锁 线程事件

  2. 2018C语言第三次作业

    要求一 2.struct sk{int a; char *str)}*p;   p->str++ 中的++ 加向? ++加向srt的地址. 要求二 题目1-计算平均成绩 1.设计思路 (1)主要 ...

  3. PHP配置xcache缓存扩展

    安装步骤 wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.gz tar -xf xcache-3.2.0.tar ...

  4. cordova 加载HTML 资源的次序

    ionic 开发的app再启动的时候不会很快,因为cordova 会加载html,css, js这些文件,读取这些文件内的代码之后,js代码才能和Native建立桥接. 这是一个初始化运行时环境的操作 ...

  5. .Net Identity OAuth 2.0 SecurityStamp 使用

    起源: 近期帮别人做项目,涉及到OAuth认证,服务端主动使token失效,要使对应用户不能再继续访问,只能重新登陆,或者重新授权. 场景: 这种场景OAuth2.0是支持的,比如用户修改了密码,那所 ...

  6. ArcGIS API for javascript开发笔记(五)——GP服务调用之GP模型的发布及使用详解

    感谢一路走来默默陪伴和支持的你~~~ ----------------欢迎来访,拒绝转载---------------- 关于GP模型的制作请点我! 一.GP发布 ArcGIS Desktop可以作为 ...

  7. ElasticSearch报 EsThreadPoolExecutor[search, queue capacity = 1000, org.elasticsearch.common.util.concurrent.EsThreadPoolExecutor@c0efba

    ElasticSearch报以下错误的解决办法: "type": "es_rejected_execution_exception", "reason ...

  8. 开工:创建虚拟机,xshell连接虚拟机,复制虚拟机,docker安装,添加加速器

    创建虚拟机:http://www.linuxidc.com/Linux/2015-08/121807.htm http://www.linuxidc.com/Linux/2010-04/25573.h ...

  9. 洛谷P2634 聪聪可可 [国家集训队] 点分治/dp

    正解:点分治/dp 解题报告: 传送门! 这题有两个做法,都是我不擅长的就都说下好了QAQ 首先这题一看到就会想到点分治? 也确实可以用点分治,那就直接用点分治鸭 每次求出到当前根距离余数为0,1,2 ...

  10. pyqt5 主界面打开新主界面、打开Dialog、打开提示框的实现模板

    import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import * ###### ...