昨天 Python释放了 3.5 ,添加了 os.scandir 根据文档该API比os.listdirDocs

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的更多相关文章

  1. 关于python中的dir函数

    dir函数用于查看python对象的属性,如果所查看的python对象已经定义了__dir__方法,则使用dir会返回定义的__dir__方法的返回值.如果没有定义__dir__方法,则会从__dic ...

  2. Python自省 type(),dir(),getattr(),hasattr(),isinstance().

    Python自省 这个也是python彪悍的特性. 自省就是面向对象的语言所写的程序在运行时,所能知道对象的类型.简单一句就是运行时能够获得对象的类型.比如type(),dir(),getattr() ...

  3. python __dict__ 跟 dir()的区别

    __dict__:要是对象的话返回的是一个对象自身的实例属性.不包括类的属性:要是类的__dict__则不包括父类的属性,只包含自身类属性[方法.类变量],不包括实例属性.正是这样.每个实例的实例属性 ...

  4. python中的 dir()内置函数的作用以及使用方法

    dir() 内置函数的作用 python 内置方法有很多,无论是初学者还是精通python 的程序员都不能全部即住所有的方法,这时候 dir() 方法就非常有用了,使用 dir()函数可以查看对象内的 ...

  5. Python Quick Start

    1.安装Python 官网下载python: https://www.python.org/ 有2.x 3.x版本, 注意,python3.0不向下兼容2.x版本,有很多包3.0不提供 下载完后直接点 ...

  6. python 日期创建dir

  7. python 中dir()和__dict__的区别

    Python __dict__与dir() 出处(http://blog.csdn.net/lis_12/article/details/53521554). Python下一切皆对象,每个对象都有多 ...

  8. python的__name__和dir()属性

    1.__name__属性 一个模块被另一个程序第一次引入时,其主程序将运行.如果我们想在模块被引入时,模块中的某一程序块不执行,我们可以用__name__属性来使该程序块仅在该模块自身运行时执行.示例 ...

  9. dir、help查询

    #!/usr/bin/env python li = [] print(dir(li)) help(list)

随机推荐

  1. jquery中的$的特殊用法

    通过父级元素选取子元素, $('父元素选择器,子元素选择器')        $('子元素选择器',父元素jquery对象); 通过$创建代码片段 $('<div/>',{ 'class' ...

  2. 设置jdk的编码

    在环境变量中:  AVA_TOOL_OPTIONS-Dfile.encoding=utf-8 -Duser.language=en -Duser.country=US

  3. music player界面

    public class SoundPlayerGUI extends JFrame implements ChangeListener, ActionListener { private stati ...

  4. Apache2 部署 Django

    环境: debian8 apache2.4.10                    #请注意自己的apache版本,不同版本配置文件结构差异很大 django1.10 python3.4 默认ap ...

  5. Python检测IP合法 是否为公网IP

    判断IP 格式是否正确 def check_value(self, ipaddr): '''检查IP是否合法 :param ipaddr: string :return True ''' addr=i ...

  6. Static List

    Static ListStatic List is the smart implementation of list data structure for those languages that h ...

  7. Thread系列之Thread.Sleep(0)

    线程这一概念,可以理解成进程中的一个小单元.这个单元是一个独立的执行单元,但是与进程中的其他线程共享进程中的内存单元. 由于Cpu资源是有限的,所以进程中的多个线程要抢占Cpu,这也导致进程中的多个线 ...

  8. openstack cloudinit 遇坑记

    我们在工作中经常会遇到创建云主机的情况,但是很少遇到给云主机改主机名的情况. 一台云主机的 hostname  一旦确定可能会涉及到很多东西,有些应用是依赖hostname的. 今天devops组的同 ...

  9. Maven间接依赖冲突解决办法

    如果项目中maven依赖太多,由于还有jar之间的间接依赖,所以可能会存在依赖冲突.依赖冲突大部分都是由于版本冲突引起的,查看maven的依赖关系,可以找到引起冲突的间接依赖 如上图,通过Depend ...

  10. WIN SERVER 2008 R2 VPN

    http://blog.csdn.net/popelovevivi/article/details/9408851 -- 还差最重要一步. 在“本地用户和组”-“用户”-右键一个你想VPN登录的用户名 ...