python os.walk()和os.path.walk()
一、os.walk()
函数声明:os.walk(top,topdown=True,onerror=None)
(1)参数top表示需要遍历的顶级目录的路径。
(2)参数topdown的默认值是“True”表示首先返回顶级目录下的文件,然后再遍历子目录中的文件。当topdown的值为"False"时,表示先遍历子目录中的文件,然后再返回顶级目录下的文件。
(3)参数onerror默认值为"None",表示忽略文件遍历时的错误。如果不为空,则提供一个自定义函数提示错误信息后继续遍历或抛出异常中止遍历。
返回值:函数返回一个元组,含有三个元素。这三个元素分别是:每次遍历的路径名、路径下子目录列表、目录下文件列表。
os.walk使用实例:删除某个文件夹(当然可以通过os.listdir的递归调用删除)
01 |
#! /usr/bin/env python |
02 |
#coding=utf-8 |
03 |
import os |
04 |
05 |
def Remove_dir(top_dir): |
06 |
if os.path.exists(top_dir)==False: |
07 |
print "not exists" |
08 |
return |
09 |
if os.path.isdir(top_dir)==False: |
10 |
print "not a dir" |
11 |
return |
12 |
for dir_path,subpaths,files in os.walk(top_dir,False): |
13 |
for file in files: |
14 |
file_path=os.path.join(dir_path,file) |
15 |
print "delete file:%s" %file_path |
16 |
os.remove(file_path) |
17 |
print "delete dir:%s" %dir_path |
18 |
os.rmdir(dir_path) |
19 |
20 |
#调用 |
21 |
|
获取文件夹大小:
import os
2 from os.path import join, getsize
3
4 def getdirsize(dir):
5 size = 0L
6 for root, dirs, files in os.walk(dir):
7 for file in files:
8 size += getsize(os.path.join(root, file))
9 print os.path.join(root,file)
10 return size
11 if __name__ == '__main__':
12 print 'sssssssssssssssssssssssssss'
13 filesize = getdirsize('/home/zhang/swift/swift')
14 print 'Ther are %.3f' % (filesize/1024/1024), 'Mbytes in ./swift'
二、os.path.walk
函数声明:os.path.walk(top,func,arg)
(1)参数top表示需要遍历的目录路径
(2)参数func表示回调函数,即对遍历路径进行处理的函数。所谓回调函数,是作为某个函数的参数使用,当某个时间触发时,程序将调用定义好的回调函数 处理某个任务。注意:walk的回调函数必须提供三个参数:第1个参数为os.path.walk的参数arg,第2个参数表示目录dirname,第3 个参数表示文件列表names。注意:os.path.walk的回调函数中的文件列表不和os.walk()那样将子目录和文件分开,而是混为了一摊, 需要在回调函数中判断是文件还是子目录。
(3)参数arg是传递给回调函数的元组,为回调函数提供处理参数,arg可以为空。回调函数的第1个参数就是用来接收这个传入的元组的。
过程:以top 为根的目录树中的每一个目录 (包含 top 自身,如果它是一个目录),以参数 (arg, dirname, names)调用回调函数 funct。参数 dirname 指定访问的目录,参数 names 列出在目录中的文件(从 os.listdir(dirname)中得到)。回调函数可以修改 names 改变 dirname 下面访问的目录的设置,例如,避免访问树的某一部分。(由 names 关连的对象必须在合适的位置被修改,使用 del 或 slice 指派。) 注意:符号连接到目录不被作为一个子目录处理,并且因此 walk()将不访问它们。访问连接的目录你必须以os.path.islink(file) 和 os.path.isdir(file)标识它们,并且必须调用walk() 。
os.path.walk使用实例:遍历文件夹下所有文件(os.path.walk()不能用于删除文件夹(可能是我没想到),因为os.path.walk()先遍历顶级目录,再遍历子目录中的文件)。
01 |
#! /usr/bin/env python |
02 |
#coding=utf-8 |
03 |
import os |
04 |
#回调函数 |
05 |
def find_file(arg,dirname,files): |
06 |
for file in files: |
07 |
file_path=os.path.join(dirname,file) |
08 |
if os.path.isfile(file_path): |
09 |
print "find file:%s" %file_path |
10 |
|
11 |
12 |
#调用 |
13 |
os.path.walk(r"C:\Users\Administrator\Desktop\4",find_file,()) |
区别:os.path.walk()与os.walk()产生的文件名列表并不相同.os.walk()产生目录树下的目录路径和文件路径,而os.path.walk()只产生文件路径(是子目录与文件的混合列表)。
python os.walk()和os.path.walk()的更多相关文章
- python 文件操作,os.path.walk()的回调函数打印文件名
#coding=utf-8 import osdef find_file(arg,dirname,files): #for i in arg: #print i for file ...
- 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.path.walk的不同
import os,os.path def func(arg,dirname,names): for filespath in names: print os.path.join(dirname,fi ...
- python使用os.listdir和os.walk获得文件的路径
python使用os.listdir和os.walk获得文件的路径 目录 情况1:在一个目录下面只有文件,没有文件夹,这个时候可以使用os.listdir 情况2:递归的情况,一个目录下面既有目录 ...
- python获取指定目录下所有文件名os.walk和os.listdir
python获取指定目录下所有文件名os.walk和os.listdir 觉得有用的话,欢迎一起讨论相互学习~Follow Me os.walk 返回指定路径下所有文件和子文件夹中所有文件列表 其中文 ...
- os.listdir()、os.walk()和os.mkdir()的用法
内容主要参照博客https://blog.csdn.net/xxn_723911/article/details/78795033 http://www.runoob.com/python/os-wa ...
- shutil.copy()、os.walk()、os.rename()实例
#!/usr/bin/python # -*- coding: UTF-8 -*- import os import shutil Path = "panel/" PNPath = ...
- 【python常用模块】os.path
os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回文件名 os.path.commonprefix(list) #返回list(多个路径) ...
- Python3.x:os.listdir和os.walk(获取路径方法)的区别
Python3.x:os.listdir和os.walk(获取路径方法)的区别 1,os.listdir 使用情况:在一个目录下面只有文件,没有文件夹,这个时候可以使用os.listdir: 例如:d ...
随机推荐
- 进程内核栈、用户栈及 Linux 进程栈和线程栈的区别
Linux 进程栈和线程栈的区别 http://www.cnblogs.com/luosongchao/p/3680312.html 总结:线程栈的空间开辟在所属进程的堆区,线程与其所属的进程共享进程 ...
- URLEncode转json
http://tool.chinaz.com/tools/urlencode.aspx?jdfwkey=zobsn2 http://www.bejson.com/
- :Hibernate逍遥游记-第16管理session和实现对话
1. package mypack; public class Monkey{ private Long id; private String name; private int count; pri ...
- Android:文件夹显示红色叹号
有感叹号,说明有的文件损坏或丢失了 解决方法: 右击工程,Build Path..->Configure Build Path...->Java Build Path 可以看到引用的jar ...
- pinyin4j
最近在倒腾与搜索相关的拼音检查技术,顺便看了一下中文转拼音开源插件pinyin4j的源码,参考资料:http://blog.csdn.net/hfhwfw/archive/2010/11/23/603 ...
- Ubuntu链接ubuntu服务器
以前在windows下用ssh工具putty连接 linux服务器, 很简单,在linux下要连接linux的服务器, 找了下,果然putty在linux中也行1,sudo apt-get insta ...
- C#获取本机IP以及无线网ip
1 private void GetIP() 2 { 3 string hostName = Dns.GetHostName();//本机名 4 //System.Net.IPAddress ...
- C#6.0 VS2015
https://msdn.microsoft.com/en-us/library/hh156499(v=vs.140).aspx This page lists key feature names f ...
- Kafka学习笔记
一.Kafka使用背景 1. Kafka的定义 是一个分布式消息系统,由LinkedIn使用Scala编写,用作LinkedIn的活动流(Activity Stream)和运营数据处理管道(Pipel ...
- 代码实现获取log日志和logcat使用方法
代码实现获取log日志new Thread(new Runnable() { @Override publi ...