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)
随机推荐
- android驱动开发前的准备(五)
搭建S3C6410开发板的测试环境 首先安装串口调试工具 第一步:检测当前系统是否支持USB转串口 # lsmod | grep usbserial 第二步:安装minicom # apt-get i ...
- 苹果Xcode 证书生成、设置、应用完整图文教程
Xcode 证书生成.设置.应用,与大家分享. 为了能够在iPhone或iPod Touch上运行iPhone应用程序,必须使用有效的数字证书签名.这个证书用于将您的开发者身份与在注册期间所提供的已确 ...
- 未能加载文件或程序集“AspNetPager”或它的某一个依赖项。参数错误(转)
未能加载文件或程序集“AspNetPager”或它的某一个依赖项.参数错误. 看你的的开发框架用的是多少的2.0, 3.0, 3.5, 4.0 那么删除的框架的文件夹也相对应的变化 删除 C:\W ...
- java批量insert入mysql数据库
mysql 批量insert语句为 insert into Table_(col1,col2...) values(val11,val12...),(val11,val12...),...; java ...
- Python 2x -> 3.x
Nowadays, Python 3 is becoming more and more popular than Python 2, but there are still a lot of cod ...
- o-sync-and-o-direct
https://lwn.net/Articles/457667/ https://lwn.net/Articles/457667/ https://lwn.net/Articles/457667/ h ...
- python socket
#!/usr/bin/env python import sys import time import socket s = socket.fromfd(sys.stdin.fileno(),sock ...
- edgesForExtendedLayout,automaticallyAdjustsScrollViewInsets, extendedLayoutIncludesOpaqueBars的影响
在IOS7以后 ViewController 开始使用全屏布局的,而且是默认的行为通常涉及到布局 就离不开这个属性 edgesForExtendedLayout,它是一个类型为UIExtendedEd ...
- kvm虚拟化平台搭建入门
KVM虚拟化有两种网络模式:1)Bridge网桥模式2)NAT网络地址转换模式Bridge方式适用于服务器主机的虚拟化.NAT方式适用于桌面主机的虚拟化. 环境: 本次实验要开启VMWare中对应Ce ...
- Squid代理服务器
缓存代理概述:做为应用层的代理服务软件,squid主要提供缓存加速,应用层过滤控制的功能. 1.代理的工作机制 当客户机通过代理来请求web页面时,指定的代理服务器会先检查自己的缓存,如果缓存中已经有 ...