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.文件操作 打开文件时,需要指定文件路径和以何等方式打开文件,打开后,即可获取该文件句柄,日后通过此文件句柄对该文件操作. 三种基本 ...
随机推荐
- s3cmd在配置后使用时提示ERROR: S3 error: 403 (InvalidAccessKeyId): The AWS Access Key Id you provided does not exist in our records.
自己新建的ceph环境,下载了s3cmd来做客户端,使用了s3cmd --configure配置后,在使用s3cmd ls可以查看到所有的bucket,但s3cmd ls s3://xxx 具体buc ...
- 数字模型制作规范(转自Unity3D群)
本文提到的所有数字模型制作,全部是用3D MAX建立模型,即使是不同的驱动引擎,对模型的要求基本是相同的.当一个VR模型制作完成时,它所包含的基本内容包括场景尺寸.单位,模型归类塌陷.命名.节点编辑, ...
- iOS - 国际化语言切换
iOS国际化:如何切换语言 1.国际化就是将标签.提示信息等信息放到资源文件中,随着程序需要的语言提供对应的资源文件.以key/value对存储,每个资源的key值不变,value随着需求改变. ...
- Google Drive 里的文件下载的方法
Google Drive 里并不提供创建直接下载链接的选项,但是可以通过小小的更改链接形式就能把分享的内容保存到本地.例如,一份通过 Google Drive 分享的文件链接形式为: https:// ...
- FZU 2252 Yu-Gi-Oh!(枚举+贪心)
Problem 2252 Yu-Gi-Oh! Accept: 105 Submit: 628 Time Limit: 1000 mSec Memory Limit : 32768 KB ...
- poj1269 intersecting lines【计算几何】
We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a p ...
- 【转】Ant与Ivy的安装
一.简介 Apache Ant,是一个将软件编译.测试.部署等步骤联系在一起加以自动化的一个工具,大多用于Java环境中的软件开发.更多介绍 Apache Ivy,是一个管理项目依赖的工具.更多介绍请 ...
- gevent 真正的协程
import gevent #第一次使用需要cmd窗口敲入 pip install Gevent from gevent import monkey:monkey.patch_all import t ...
- 企业证书安装App
通过苹果自带的浏览器访问:itms-services:///?action=download-manifest&url=https://www.xxxx.com:xxx/xxxx/xxx.pl ...
- element 表格元素 超链接
1.在循环体中的事件绑定 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...