Python Quick list dir
昨天 Python释放了 3.5 ,添加了 os.scandir
根据文档该API比os.listdir
快Docs
which speeds it up by 3-5 times on POSIX systems and by 7-20 times on Windows systems
以前因为目录太大(文件数过万),listdir又太慢,写了一个自己的listdir,发布一下 (仅支持Linux)
#!/usr/bin/python
import os
import ctypes
from ctypes.util import find_library
clib = ctypes.CDLL(find_library('C'))
class c_dir(ctypes.Structure):
pass
class c_dirent(ctypes.Structure):
_fields_ = (
('d_ino', ctypes.c_long),
('d_off', ctypes.c_long), # offset
('d_reclen', ctypes.c_ushort), # record length
('d_type', ctypes.c_byte),
('d_name', ctypes.c_char *4096),
)
c_dir_p = ctypes.POINTER(c_dir)
c_dirent_p = ctypes.POINTER(c_dirent)
opendir, readdir, closedir = clib.opendir, clib.readdir, clib.closedir
opendir.argtypes = [ ctypes.c_char_p ]
opendir.restype = c_dir_p
readdir.argtypes = [ c_dir_p ]
readdir.restype = c_dirent_p
closedir.argtypes = [ c_dir_p ]
closedir.restype = ctypes.c_int
def countdir(path):
if not os.path.isdir(path):
raise ValueError('arg error, not a dir: '+path)
dirfd = opendir(path)
total_num, total_filename, total_metasize = 0, 0, 0
try:
while True:
entry = readdir(dirfd)
if not entry:
break
total_filename += len(entry.contents.d_name)
total_metasize += entry.contents.d_reclen
total_num += 1
finally:
closedir(dirfd)
return {"count":total_num-2, "total_filename":total_filename, "total_metasize":total_metasize,"dirsize":os.path.getsize(path)}
def listdir(path):
'include two special dirs: . and .. '
if not os.path.isdir(path):
raise ValueError('arg error, not a dir: '+path)
dirfd = opendir(path)
try:
while True:
entry = readdir(dirfd)
if not entry:
break
yield {"name":entry.contents.d_name,
"inode": entry.contents.d_ino,
"metasize":entry.contents.d_reclen}
finally:
closedir(dirfd)
if __name__ == '__main__':
import sys
i = 0
total = 0
path = sys.argv[1]
print( countdir(path) )
# for entry in listdir(path):
# print(entry['name'], entry['metasize'])
# total += entry['metasize']
# print('total:', total, 'dir size: ', os.path.getsize(path))
Python Quick list dir的更多相关文章
- 关于python中的dir函数
dir函数用于查看python对象的属性,如果所查看的python对象已经定义了__dir__方法,则使用dir会返回定义的__dir__方法的返回值.如果没有定义__dir__方法,则会从__dic ...
- Python自省 type(),dir(),getattr(),hasattr(),isinstance().
Python自省 这个也是python彪悍的特性. 自省就是面向对象的语言所写的程序在运行时,所能知道对象的类型.简单一句就是运行时能够获得对象的类型.比如type(),dir(),getattr() ...
- python __dict__ 跟 dir()的区别
__dict__:要是对象的话返回的是一个对象自身的实例属性.不包括类的属性:要是类的__dict__则不包括父类的属性,只包含自身类属性[方法.类变量],不包括实例属性.正是这样.每个实例的实例属性 ...
- python中的 dir()内置函数的作用以及使用方法
dir() 内置函数的作用 python 内置方法有很多,无论是初学者还是精通python 的程序员都不能全部即住所有的方法,这时候 dir() 方法就非常有用了,使用 dir()函数可以查看对象内的 ...
- Python Quick Start
1.安装Python 官网下载python: https://www.python.org/ 有2.x 3.x版本, 注意,python3.0不向下兼容2.x版本,有很多包3.0不提供 下载完后直接点 ...
- python 日期创建dir
- python 中dir()和__dict__的区别
Python __dict__与dir() 出处(http://blog.csdn.net/lis_12/article/details/53521554). Python下一切皆对象,每个对象都有多 ...
- python的__name__和dir()属性
1.__name__属性 一个模块被另一个程序第一次引入时,其主程序将运行.如果我们想在模块被引入时,模块中的某一程序块不执行,我们可以用__name__属性来使该程序块仅在该模块自身运行时执行.示例 ...
- dir、help查询
#!/usr/bin/env python li = [] print(dir(li)) help(list)
随机推荐
- 通过配置文件启动odoo-10.0
odoo-10.0文件夹中找到odoo-bin,这是10.0的启动文件,也是一个py文件 使用pycharm启动后,找到run->edit configuration 找到odoo-bin 找到 ...
- SDWebImage
SDWebImage 加载图片的流程 入口 setImageWithURL:placeholderImage:options: 会先把 placeholderImage 显示,然后 SDWebImag ...
- Android 图片上传
上传方式:两种 1:Base64() (1):获取图片路径,将图片转为String 类型 (2):通过post提交的方式.以键值对的方式上传到服务器,和一般的提交关键字没有任何区别. (3):这种 ...
- 备忘录模式(Memento Pattern)
在不破坏封闭的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态. 备忘录模式主要思想是——利用备忘录对象来对保存发起人的内部状态,当发起人需要恢复原 ...
- Linux环境下段错误的产生原因及调试方法小结(转)
最近在Linux环境下做C语言项目,由于是在一个原有项目基础之上进行二次开发,而且 项目工程庞大复杂,出现了不少问题,其中遇到最多.花费时间最长的问题就是著名的“段错误”(Segmentation F ...
- asp.net 自定义控件 嵌入资源文件 备忘
要想在自定义用户控件中嵌入资源,从以下几个步骤入手: 1.在AssemblyInfo.cs中注册资源,文件夹层级用点隔开.例如: [assembly: System.Web.UI.WebResourc ...
- css预处理语言的模块化实践
编写css是前端工作中,一项普通而又频繁的劳动,由于css并不是一门语言,所以在程序设计上显得有些简陋.对于小型项目来说,css的量还不至于庞大,问题没有凸显,而如果要开发和持续维护一个较为大型的项目 ...
- python3-day2-python基础2
一.for循环 for循环是我们编程中非常常用的一种循环,以下就是for循环在python中的一些应用实例: 1.单层for循环 #!/usr/bin/env python3#-*- coding: ...
- python之 Redis
Redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorte ...
- [综] Latent Dirichlet Allocation(LDA)主题模型算法
多项分布 http://szjc.math168.com/book/ebookdetail.aspx?cateid=1&§ionid=983 二项分布和多项分布 http:// ...