python 文件操作,os.path.walk()的回调函数打印文件名
#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()的回调函数打印文件名的更多相关文章
- 关于python 文件操作os.fdopen(), os.close(), tempfile.mkstemp()
嗯.最近在弄的东西也跟这个有关系,由于c基础渣渣.现在基本上都忘记得差不多的情况下,是需要花点功夫才能弄明白. 每个语言都有相关的文件操作. 今天在flask 的例子里看到这样一句话.拉开了文件操作折 ...
- Python基本知识 os.path.join与split() 函数
Python中有join和os.path.join()两个函数,具体作用如下: join:连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符)连接生成一个新的字符串os.path.joi ...
- python文件操作os模块
Python 统计某一文件夹下文件数量 使用python pathlib模块 from pathlib import Path dir_path = ' ' print(len(list(Path( ...
- python os.walk()和os.path.walk()
一.os.walk() 函数声明:os.walk(top,topdown=True,onerror=None) (1)参数top表示需要遍历的顶级目录的路径. (2)参数topdown的默认值是“Tr ...
- Python:os.walk()和os.path.walk()用法
转于:https://www.cnblogs.com/zmlctt/p/4222621.html 博主:zmlctt 一.os.walk() 函数声明:os.walk(top,topdown=True ...
- python(3):文件操作/os库
文件基本操作 r,以读模式打开, r+=r+w, w, 写模式(清空原来的内容), w+=w+r, a , 追加模式, a+=a+r, rb, wb, ab, b表示以二进制文件打开 想在一段文 ...
- day18 时间:time:,日历:calendar,可以运算的时间:datatime,系统:sys, 操作系统:os,系统路径操作:os.path,跨文件夹移动文件,递归删除的思路,递归遍历打印目标路径中所有的txt文件,项目开发周期
复习 ''' 1.跨文件夹导包 - 不用考虑包的情况下直接导入文件夹(包)下的具体模块 2.__name__: py自执行 '__main__' | py被导入执行 '模块名' 3.包:一系列模块的集 ...
- 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 ...
- python文件操作及os模块常用命令
1.文件打开 文件句柄 = open('文件路径', '模式') 2.文件操作 打开文件时,需要指定文件路径和以何等方式打开文件,打开后,即可获取该文件句柄,日后通过此文件句柄对该文件操作. 三种基本 ...
随机推荐
- Unity3D笔记 愤怒的小鸟<六> 弹弓发射小鸟
要实现的目标 实现个性化的鼠标 实现弹弓 选择小鸟.拉升弹弓.发射小鸟 弹弓橡皮筋 声音 1.实现个性化鼠标 效果 2.添加弹弓 建立两个材质 创建一个空GameObject 把两个shoot拖进来统 ...
- java try catch 异常后还会继续执行吗
catch 中如果你没有再抛出异常 , 那么catch之后的代码是可以继续执行的 , 但是try中 , 报错的那一行代码之后 一直到try结束为止的这一段代码 , 是不会再执行的. ========= ...
- git如何回滚当前修改的内容?
git如何回滚当前修改的内容? 1.打开git gui,在工具栏上点击 commit ,选择 Revert Changes, 这里可以回滚单个文件: 2.一键回滚所有修改: 打开git gui,在工 ...
- css如何设置div中的内容垂直居中?
<style>.out { position: relative;//相对div的定位 top: 30%;//相对div的border-top的距离,根据元素的高度,50%则为垂直居中:} ...
- ggplot2绘制概率密度图
以下绘图以Weibull分布(韦伯分布.威布尔分布)为例 关于Weibull分布(韦伯分布.威布尔分布),请参考本人博客http://www.cnblogs.com/wwxbi/p/6141501.h ...
- POJ-放苹果(DP)
放苹果 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29074 Accepted: 18376 Description 把M个 ...
- JNUOJ 1184 - 科学计数法
花了半个小时,强行拗出一长串又臭又长的代码,把所有情况都分了(该分的,不该分的……都分了……) #include<cstdio> #include<cstring> #incl ...
- 拓扑_dfs——找最小环
今天在题库发现了一个wa了很久还没调过的题,这个题呢是2015年noip的day1t2,莫名感觉难度上升(其实水的一匹). 这道题输出是3,其实就是一个图中让你找最小环,尽管我不会找环,但是要是我的话 ...
- python MD5步骤
https://www.cnblogs.com/zipon/p/8340720.html import hashlib def get_token(): md5str = "abc" ...
- 20144306《网络对抗》MAL_免杀原理与实践
一.基础问题回答 1.杀软是如何检测出恶意代码的? (1)特征码:类似于人的生物特征,恶意代码可能会包含一段或多端数据能代表其特征.杀软一般会对文件内容进行静态扫描,将文件内容与特征库进行匹配,来检测 ...