Python入门之os.walk()方法
os.walk方法,主要用来遍历一个目录内各个子目录和子文件。
os.walk(top, topdown=True, onerror=None, followlinks=False)
可以得到一个三元tupple(dirpath, dirnames, filenames),
第一个为起始路径,第二个为起始路径下的文件夹,第三个是起始路径下的文件。
dirpath 是一个string,代表目录的路径,
dirnames 是一个list,包含了dirpath下所有子目录的名字。
filenames 是一个list,包含了非目录文件的名字。
这些名字不包含路径信息,如果需要得到全路径,需要使用os.path.join(dirpath, name).
通过for循环自动完成递归枚举
例如:
F:\aaa 目录是这样的文件目录结构
F:\aaa
|--------1.txt
|--------2.txt
|--------3.txt
|--------4
|-------5.txt
|-------6.txt
|-------7.txt
分别打印各个参数的实际值
#!/usr/bin/env python
# 2.py
# use UTF-8
# Python 3.3.0 # os.walk()的使用
import os # 枚举dirPath目录下的所有文件 def main():
#begin
fileDir = "F:" + os.sep + "aaa" # 查找F:\aaa 目录下
for root, dirs, files in os.walk(fileDir):
#begin
print(root)
print(dirs)
print(files)
#end
os.system("pause")
#end if __name__ == '__main__':
#begin
main()
#end # 输出
# F:\aaa
# ['4']
# ['1.txt', '2.txt', '3.txt']
# F:\aaa\4
# []
# ['5.txt', '6.txt', '7.txt']
你也可以这样,用元组a,通过for循环即可完成目录的递归.
#!/usr/bin/env python
# 3.py
# use UTF-8
# Python 3.3.0 # os.walk()的使用
import os # 枚举dirPath目录下的所有文件 def main():
#begin
fileDir = "F:" + os.sep + "aaa" # 查找F:\aaa 目录下
for a in os.walk(fileDir):
#begin
print(a[0])
print(a[1])
print(a[2])
#end
os.system("pause")
#end if __name__ == '__main__':
#begin
main()
#end # 输出
# F:\aaa
# ['4']
# ['1.txt', '2.txt', '3.txt']
# F:\aaa\4
# []
# ['5.txt', '6.txt', '7.txt']
你还可以这样,先打印目录,再打印各个文件
#!/usr/bin/env python
# 2.py
# use UTF-8
# Python 3.3.0 # os.walk()的使用
import os # 枚举dirPath目录下的所有文件 def main():
#begin
fileDir = "F:" + os.sep + "aaa" # 查找F:\aaa 目录下
for root, dirs, files in os.walk(fileDir):
#begin
for dir in dirs:
#begin
print(os.path.join(root, dir))
#end
for file in files:
#begin
print(os.path.join(root, file))
#end
#end
os.system("pause")
#end if __name__ == '__main__':
#begin
main()
#end # 输出
# F:\aaa\4
# F:\aaa\1.txt
# F:\aaa\2.txt
# F:\aaa\3.txt
# F:\aaa\4\5.txt
# F:\aaa\4\6.txt
# F:\aaa\4\7.txt
Python入门之os.walk()方法的更多相关文章
- Python os.walk() 方法遍历文件目录
概述 os.walk() 方法用于通过在目录树中游走输出在目录中的文件名,向上或者向下. os.walk() 方法是一个简单易用的文件.目录遍历器,可以帮助我们高效的处理文件.目录方面的事情. 在Un ...
- Python 入门 之 双下方法
Python 入门 之 双下方法 1.双下方法 定义:双下方法是特殊方法,它是解释器提供的 由双下划线加方法名加双下划线 方法名的具有特殊意义的方法,双下方法主要是python源码程序员使用的,我 ...
- python os.walk()方法--遍历当前目录的方法
前记:有个奇妙的想法并想使用代码实现,发现了一个坑,百度了好久也没发现的"填坑"的文章~~~~~~~~~ 那就由我来填 os.walk()支持相对路径 例如 os.walk(&qu ...
- Python os.walk() 方法
#coding=utf-8 import os #(dirpath, dirnames, filenames)[文件夹路径, 文件夹名字, 文件名] def file_name(file_dir): ...
- python中的os.walk
原文出处:https://www.jianshu.com/p/bbad16822eab python中os.walk是一个简单易用的文件.目录遍历器,可以帮助我们高效的处理文件.目录方面的事情. 1. ...
- python遍历目录os.walk(''d:\\test2",topdown=False)
os.walk(top, topdown=True, onerror=None, followlinks=False)遍历目录,topdown=false表示先返回目录,后返回文件 参数说明: top ...
- python学习之os.walk()
os.walk(top,topdown = True,onerror = None,followlinks = False) 参数 top -- 根目录下的每一个文件夹(包含它自己), 产生3-元组 ...
- python入门之两种方法修改文件内容
1.占硬盘修改 import os file_name="兼职.txt" new_file_name="%s.new".% file_name old_str= ...
- python入门之os模块
import os os.getcwd() 同Linux的pwd os.chdir("/opt") 同Linux的cd os.curdir 返回当前目录 os.pardir 获取上 ...
随机推荐
- poj2406 Power Strings 【KMP】
Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc&quo ...
- R数据可视化手册学习简单的绘制常见的图形
1.绘制散点图 # 使用ggplot2 library(ggplot2) ggplot(data = mtcars, aes(x = wt, y = mpg)) + geom_point() 2.绘制 ...
- 各大互联网公司java开发面试常问问题
本人是做java开发的,这是我参加58,搜狐,搜狗,新浪微博,百度,腾讯文学,网易以及其他一些小的创业型公司的面试常被问的问题,当然有重复,弄清楚这些,相信面试会轻松许多. 1. junit用法,be ...
- Oracle安装部署之Oracle 10g在redhat5下的安装
[root@localhost ~]# groupadd dba -g 111 [root@localhost ~]# groupadd oinstall -g 110 [root@localhost ...
- python面向对象高级:__slots__
__slots__ 一个在有着数以千计的对象的类的时候节省内存的方法. 在Python中,每个类都有实例属性.默认情况下Python用一个字典来保存一个对象的实例属性.这非常有用,因为它允许我们在运行 ...
- is_link
'Symbolic Link' to File1 content containing path to File1'Hard Link' to File1 content containing Fi ...
- android call and audio
mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system ConnCtl *:s android audio (http://blog.cs ...
- Arrow-一个最好用的日期时间Python处理库
https://www.jianshu.com/p/c878bb1c48c1 写过Python程序的人大都知道,Python日期和时间的处理非常繁琐和麻烦,主要有以下几个问题: 有众多的package ...
- sklearn学习总结(超全面)
https://blog.csdn.net/fuqiuai/article/details/79495865 前言sklearn想必不用我多介绍了,一句话,她是机器学习领域中最知名的python模块之 ...
- sql中charindex的用法
转自:https://www.cnblogs.com/beeone/p/3621743.html CHARINDEX和PATINDEX函数常常用来在一段字符中搜索字符或者字符串.如果被搜索的字符中包含 ...