Python 用 os.walk 遍历目录
今天第一次进行 文件遍历,自己递归写的时候还调试了好久,(主要因为分隔符号的问题),后来发现了os.walk方法,就忍不住和大家分享下.
先看下代码:
import os
for i in os.walk('c:'+os.sep+'ant'):
print i[1]
下面是输出:
c:\ant
c:\ant\bin
c:\ant\docs
c:\ant\docs\ant2
c:\ant\docs\antlibs
c:\ant\docs\antlibs\antunit
c:\ant\docs\antlibs\compress
c:\ant\docs\antlibs\dotnet
c:\ant\docs\antlibs\props
c:\ant\docs\antlibs\svn
c:\ant\docs\images
c:\ant\docs\manual
c:\ant\docs\manual\api
c:\ant\docs\manual\api\org
c:\ant\docs\manual\api\org\apache
c:\ant\docs\manual\api\org\apache\tools
c:\ant\docs\manual\api\org\apache\tools\ant
c:\ant\docs\manual\api\org\apache\tools\ant\dispatch
c:\ant\docs\manual\api\org\apache\tools\ant\filters
后面还有很长.
如果不使用这个方法,遍历同样能达到效果.不过使用 os.walk 方便很多了.这个方法返回的是一个三元tupple(dirpath, dirnames, filenames),
其中第一个为起始路径,
第二个为起始路径下的文件夹,
第三个是起始路径下的文件.
dirpath是一个string,代表目录的路径,
dirnames是一个list,包含了dirpath下所有子目录的名字,
filenames是一个list,包含了非目录文件的名字.这些名字不包含路径信息,如果需要得到全路径,需要使用 os.path.join(dirpath, name).
下面是可以看到 os.walk 方法返回的内容.
代码:
import os
for i in os.walk('c:'+os.sep+'ant'):
print i
输出:
('c:\\ant', ['bin', 'docs', 'etc', 'lib', 'Project'], ['fetch.xml', 'get-m2.xml', 'INSTALL', 'KEYS', 'LICENSE', 'NOTICE', 'README', 'WHATSNEW'])
('c:\\ant\\bin', [], ['ant', 'ant.bat', 'ant.cmd', 'antenv.cmd', 'antRun', 'antRun.bat', 'antRun.pl', 'complete-ant-cmd.pl', 'envset.cmd', 'lcp.bat', 'runant.pl', 'runant.py', 'runrc.cmd'])
('c:\\ant\\docs', ['ant2', 'antlibs', 'images', 'manual', 'projects', 'slides', 'webtest'], ['antnews.html', 'ant_in_anger.html', 'ant_task_guidelines.html', 'appendix_e.pdf', 'breadcrumbs.js', 'bugs.html', 'bylaws.html', 'contributors.html', 'external.html', 'faq.html', 'favicon.ico', 'index.html', 'legal.html', 'LICENSE', 'license.html', 'mail.html', 'mission.html', 'nightlies.html', 'page.css', 'problems.html', 'projects.html', 'resources.html', 'svn.html'])
('c:\\ant\\docs\\ant2', [], ['actionlist.html', 'features.html', 'FunctionalRequirements.html', 'original-specification.html', 'requested-features.html', 'requested-features.txt', 'VFS.txt'])
('c:\\ant\\docs\\antlibs', ['antunit', 'compress', 'dotnet', 'props', 'svn'], ['bindownload.cgi', 'bindownload.html', 'charter.html', 'index.html', 'proper.html', 'sandbox.html', 'srcdownload.cgi', 'srcdownload.html'])
('c:\\ant\\docs\\antlibs\\antunit', [], ['index.html'])
('c:\\ant\\docs\\antlibs\\compress', [], ['index.html'])
('c:\\ant\\docs\\antlibs\\dotnet', [], ['index.html'])
('c:\\ant\\docs\\antlibs\\props', [], ['index.html'])
...
当然后面还有很长了.
有了这个函数无论是遍历文件夹,还是遍历文件都很方便.
下面是我是自己用递归实现的遍历文件方法.
代码:
def listdir(leval,path):
for i in os.listdir(path):
print('| '*(leval + 1) + i)
if os.path.isdir(path+i):
listdir(leval+1, path+i)
path = 'c:'+os.sep+'ant'
#或者直接 path='C:/ant'
print(path+os.sep)
listdir(0, path+os.sep)
下面是输出:
c:\ant\
| bin
| | ant
| | ant.bat
| | ant.cmd
| | antenv.cmd
| | antRun
| | antRun.bat
| | antRun.pl
| | complete-ant-cmd.pl
| | envset.cmd
| | lcp.bat
| | runant.pl
| | runant.py
| | runrc.cmd
| docs
| | ant2
| | antlibs
| | antnews.html
| | ant_in_anger.html
| | ant_task_guidelines.html
| | appendix_e.pdf
| | breadcrumbs.js
| | bugs.html
| | bylaws.html
| | contributors.html
| | external.html
| | faq.html
| | favicon.ico
| | images
| | index.html
| | legal.html
| | LICENSE
| | license.html
| | mail.html
| | manual
| | mission.html
| | nightlies.html
| | page.css
| | problems.html
| | projects
| | projects.html
| | resources.html
| | slides
| | svn.html
| | webtest
| etc
| | ant-bootstrap.jar
| | changelog.xsl
| | checkstyle
| | coverage-frames.xsl
| | jdepend-frames.xsl
| | jdepend.xsl
| | junit-frames-xalan1.xsl
| | junit-frames.xsl
| | junit-noframes.xsl
| | log.xsl
| | maudit-frames.xsl
| | mmetrics-frames.xsl
| | tagdiff.xsl
| fetch.xml
| get-m2.xml
| INSTALL
| KEYS
| lib
| | ant-1.8.0.pom
| | ant-1.8.0.pom.md5
| | ant-1.8.0.pom.sha1
| | ant-1.8.0.pom.sha512
..
如果只想得到文件夹,而不要文件,把要做的事情放到
if os.path.isdir(path+i):
里面就好了,比如: print()
O(∩_∩)O~
Python 用 os.walk 遍历目录的更多相关文章
- python中os.walk()遍历目录中所有文件
之前一直用判断目录和文件的递归方法来获取一个目录下的所有文件,后来发现python里面已经写好了这个函数,不需要自己递归获取了,记录下os.walk()函数的用法 目的:获取path下所有文件,返回由 ...
- python通过os.walk() 遍历出多级目录下所有文件绝对路径
代码如下 将遍历出来的路径全部添加到列表中: def get_all_abs_path(source_dir): path_list = [] for fpathe, dirs, fs in os.w ...
- python中os.walk浏览目录和文件
#!/usr/bin/env python # 2.py # use UTF-8 # Python 3.3.0 # os.walk()的使用 import os # 枚举dirPath目录下的所有文件 ...
- 利用 os.walk() 遍历目录
os.walk: walk(top, topdown=True, onerror=None, followlinks=False) 参数: top 要遍历的目录地址 topdown 为真,则优先遍历t ...
- Python 使用 os 模块遍历目录/获取当前文件的路径
1.列出指定目录下所包含的目录 item = os.listdir("/Users/jinchengxie/go") 返回的是一个列表, 里面包含了指定目录下所包含的所有的目录 2 ...
- os.walk() 遍历目录下的文件夹和文件
os.walk(top, topdown=True, onerror=None, followlinks=False) top:顶级目录 os.walk()返回一个三元tupple(dirpath, ...
- 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.path.walk()用法
转于:https://www.cnblogs.com/zmlctt/p/4222621.html 博主:zmlctt 一.os.walk() 函数声明:os.walk(top,topdown=True ...
- Python模块 os.walk
Os.walk os.walk(top,topdown=True,onerror=None,followlinks=False) os.walk()是python中内置(built-in)的目录树生成 ...
随机推荐
- Initialization and Class loading - Java
可以说,类的代码在初次使用时才加载.这通常指加载发生于创建类的第一个对象之时,但当访问 static域或static方法时,也会发生加载(通过下面的这段代码验证). class LoadTest { ...
- Data Base MySQL的常用命令
MySQL的常用命令 一.下载地址: http://www.mysql.com 二.安装注意: root默认密码:123456 三.常用命令: 1.创建用户并授权: 创建用户,只能本地访问:cr ...
- makefile中的自动化变量 【转】
转自:http://blog.chinaunix.net/uid-28458801-id-3495215.html 自动化变量 模式规则中,规则的目标和依赖文件名代表了一类文件名:规则的命令是对所有这 ...
- linux中/etc/init.d [转]
一.关于/etc/init.d 如果你使用过linux系统,那么你一定听说过init.d目录.这个目录到底是干嘛的呢?它归根结底只做了一件事情,但这件事情非同小可,是为整个系统做的,因此它非常重要.i ...
- 使用Gson解析复杂的json数据
Gson解析复杂的json数据 最近在给公司做一个直播APK的项目,主要就是通过解析网络服务器上的json数据,然后将频道地址下载下来再调用Android的播放器进行播放,原先本来打算使用普通的jso ...
- windows服务删除后,在次安装时无法安装启动。
当我在windows的cmd下卸载evtsys evtsys -u 再次安装时evtsys.exe -i -h 192.168.32.12 -p 514 提示“指定的服务已标记为删除”,进入服务管理 ...
- Linux常用到的指令汇总
Linux常用到的指令汇总 根据鸟哥linux私房菜上定义的:一定要先學會的指令:ls, more, cd, pwd, rpm, ifconfig, find 登入與登出(開機與關機):telnet, ...
- mongodb添加用户和认证
Mongodb默认启动是不带认证,也没有账号,只要能连接上服务就可以对数据库进行各种操作,这样可不行.现在,我们得一步步开启使用用户和认证. 第一步,我们得定位到mongodb的安装目录.我本机的是C ...
- Android 工程在4.0基础上混淆
Android现在对安全方面要求比较高了,我今天要做的对apk进行混淆,用所有的第三方工具都不能反编译,作者的知识产权得到保障了,是不是碉堡了. 一,首先说明我这是在4.0基础上进行的. 先看看pro ...
- windows主机无法访问服务器
在CentOS下安装配置hadoop的时候,总显示因为网络端口而导致主机无法访问服务器,遂打算先关闭一下防火墙. 偶然看到防火墙的配置操作说明,感觉不错.执行”setup”命令启动文字模式配置实用程序 ...