python-os.walk()使用举例
文件目录结构
dir
1
1
1.txt
2.txt
3.txt
2
2.txt
3
4
4.txt
3.txt
1.txt
2
2.txt
3
3.txt
dir.txt
代码:
import os def main():
#InputDir="D:\python\dir"
InputDir="dir"
print(InputDir)
for root,dirs,files in os.walk(InputDir):
for filename in files:
#fullPath=os.path.join(dirpath, filename)
fullFilename = root +'/' + filename
print(fullFilename) main()
执行结果:
dir
dir/dir.txt
dir\1/1.txt
dir\1\1/1.txt
dir\1\1/2.txt
dir\1\1/3.txt
dir\1\2/2.txt
dir\1\3/3.txt
dir\1\3\4/4.txt
dir\2/2.txt
dir\3/3.txt
从结果中可以看到它是按照深度方式遍历文件的。
参考:
http://blog.csdn.net/b_h_l/article/details/11241841
python-os.walk()使用举例的更多相关文章
- Python os.walk文件遍历用法【转】
python中os.walk是一个简单易用的文件.目录遍历器,可以帮助我们高效的处理文件.目录方面的事情. 1.载入 要使用os.walk,首先要载入该函数 可以使用以下两种方法 import os ...
- Python os.walk的用法与举例
os.walk(top, topdown=True, onerror=None, followlinks=False) 可以得到一个三元tupple(dirpath, dirnames, filena ...
- python os.walk()和os.path.walk()
一.os.walk() 函数声明:os.walk(top,topdown=True,onerror=None) (1)参数top表示需要遍历的顶级目录的路径. (2)参数topdown的默认值是“Tr ...
- Python os.walk() 方法遍历文件目录
概述 os.walk() 方法用于通过在目录树中游走输出在目录中的文件名,向上或者向下. os.walk() 方法是一个简单易用的文件.目录遍历器,可以帮助我们高效的处理文件.目录方面的事情. 在Un ...
- python os.walk详解
os模块大全详情 os.walkos.walk方法,主要用来遍历一个目录内各个子目录和子文件. os.walk(top, topdown=True, onerror=None, followlinks ...
- Python os.walk文件遍历
os.walk(top, topdown=True, onerror=None, followlinks=False) 可以得到一个三元tupple(dirpath, dirnames, filena ...
- Python os.walk() 简介
Table of Contents 1. os.walk目录遍历 1.1. os.walk 1.2. 例子 1.2.1. 测试topdown 1.2.2. 运行时修改遍历目录 2. 参考资料 os.w ...
- python os.walk()
os.walk()返回三个参数:os.walk(dirpath,dirnames,filenames) for dirpath,dirnames,filenames in os.walk(): 返回d ...
- python os.walk()遍历
os.walk()遍历 import os p='/bin' #设定一个路径 for i in os.walk(p): #返回一个元组 print (i) # i[0]是路径 i[1]是文件夹 i[2 ...
- python os.walk()方法--遍历当前目录的方法
前记:有个奇妙的想法并想使用代码实现,发现了一个坑,百度了好久也没发现的"填坑"的文章~~~~~~~~~ 那就由我来填 os.walk()支持相对路径 例如 os.walk(&qu ...
随机推荐
- 按照HashMap中value值进行排序
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; ...
- You're trying to decode an invalid JSON String JSON返回有解析问题
SpringMVC架构的web程序,通常用map返回消息在浏览器中显示,但是实际中报下列错误“”You're trying to decode an invalid JSON String“返回的字符 ...
- Performance Tuning Using Linux Process Management Commands
[root@bigdata-server-02 /]# ps --help all Usage: ps [options] Basic options: -A, -e all processes -a ...
- Rethinking the Inception Architecture for Computer Vision
https://arxiv.org/abs/1512.00567 Convolutional networks are at the core of most state-of-the-art com ...
- ABAP 通过字段找表程序
2.获取数据保存在哪个数据表的方法: 1.前台对指定栏位 使用F1帮助找表,2.st05 跟踪业务操作过程,检索需要的数据表,(此方法找表很高效)3.对于文本字段找表,可以找到前台维护处,->维 ...
- 【LeetCode】Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【Effective C++】让自己习惯C++
条款01:视C++为一个语言联绑 C++的四个语言层次: C:C++是以C为基础的.基本数据类型.语句.预处理器.数组.指针等统统来自C. Oject-Oriented C++:面向对象这一特性包含了 ...
- android SDK中添加自定义api【转】
本文转载自:http://blog.csdn.net/a624731186/article/details/23548409 本文的思路: android APP调用自定义java API,自定义ja ...
- IPFS 到底是怎么工作的?
简介 我们知道,一个存储服务,最基本的功能就是存和取.IPFS 中提供了这两种语义,那就是 add 和 get 操作. 在 IPFS 系统中执行 add 操作,就是执行了一次存操作,放在网络的概念里, ...
- the art of seo(chapter one)
preface:Andy Johns (@ibringtraffic):growth strategist@Wealthfront ***1.Search Reflecting Consciousne ...