os.listdir的解决方法OSError [英] Workaround OSError with os.listdir

查看:365
本文介绍了os.listdir的解决方法OSError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含90K文件的目录.如此荒谬的文件数量如此之多,以至于bash之类的功能如ls都失败了.因此,当然,我的python(Mac Python,版本2.5)脚本中的os.listdir()也是;它失败,并显示OSError: [Errno 12] Cannot allocate memory: '.'

I have a directory with 90K files in it. This is such a preposterously huge number of files that bash functions like ls fail. So of course, does os.listdir() from my python (Mac Python, version 2.5) script; it fails with OSError: [Errno 12] Cannot allocate memory: '.'

人们会说:不要将那么多文件放在一个目录中!您疯了吗?" -但我想假装自己生活在未来,这是一个光彩夺目的地方,那里有数以万计的内存供我使用,并且只要文件存在,就不必担心文件的确切位置我的旋转盘上还留着锈.

People will say "Don't put that many files in a single directory! Are you crazy?" -- but I like to pretend that I live in the future, a brilliant, glowing place, where I have gigabytes of memory at my disposal, and don't need to worry too much about where exactly my files go, as long as there's rust left on my spinning platters.

那么,对于这个os.listdir()问题,有很好的解决方法吗?我考虑过仅使用find,但这有点麻烦,不幸的是,find是递归的,在Mac OS X 10.6上没有受支持的maxdepth选项.

So, is there a good workaround for this os.listdir() problem? I've considered just shelling out to find, but that's a bit gross, and unfortunately find is recursive, with no supported maxdepth option on Mac OS X 10.6.

下面是通过掏空os.listdir来查找的样子:

Here's what the os.listdir via shelling out to find looks like, roughly:

def ls(directory): 
    import os
    files = os.popen4('find %s' % directory)[1].read().rstrip().split('\n')
    files.remove(directory)
    return files # probably want to remove dir prefix from everything in here too

更新:os.listdir()在python 2.6中成功.

Update: os.listdir() succeeds in python 2.6.

推荐答案

def ls(directory): 
    """full-featured solution, via wrapping find"""
    import os
    files = os.popen4('find %s' % directory)[1].read().rstrip().split('\n')
    files.remove(directory)
    n = len(directory)
    if directory[-1] != os.path.sep:
        n += 1
    files = [f[n:] for f in files] # remove dir prefix
    return [f for f in files if os.path.sep not in f] # remove files in sub-directories

这篇关于os.listdir的解决方法OSError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆