Python的Directory Walker

Spe*_*cto 5 python directory-listing

我目前正在使用Here的目录walker

import os
class DirectoryWalker:
# a forward iterator that traverses a directory tree

def __init__(self, directory):
    self.stack = [directory]
    self.files = []
    self.index = 0

def __getitem__(self, index):
    while 1:
        try:
            file = self.files[self.index]
            self.index = self.index + 1
        except IndexError:
            # pop next directory from stack
            self.directory = self.stack.pop()
            self.files = os.listdir(self.directory)
            self.index = 0
        else:
            # got a filename
            fullname = os.path.join(self.directory, file)
            if os.path.isdir(fullname) and not os.path.islink(fullname):
                self.stack.append(fullname)
            return fullname

for file in DirectoryWalker(os.path.abspath('.')):
    print file
Run Code Online (Sandbox Code Playgroud)

此次要更改允许您在文件中拥有完整路径.

任何人都可以帮助我如何使用这个来找到文件名?我需要完整的路径,只需要文件名.

Che*_*ery 12

你为什么要自己做这种无聊的事情?

for path, directories, files in os.walk('.'):
    print 'ls %r' % path
    for directory in directories:
        print '    d%r' % directory
    for filename in files:
        print '    -%r' % filename
Run Code Online (Sandbox Code Playgroud)

输出:

'.'
    d'finction'
    d'.hg'
    -'setup.py'
    -'.hgignore'
'./finction'
    -'finction'
    -'cdg.pyc'
    -'util.pyc'
    -'cdg.py'
    -'util.py'
    -'__init__.pyc'
    -'__init__.py'
'./.hg'
    d'store'
    -'hgrc'
    -'requires'
    -'00changelog.i'
    -'undo.branch'
    -'dirstate'
    -'undo.dirstate'
    -'branch'
'./.hg/store'
    d'data'
    -'undo'
    -'00changelog.i'
    -'00manifest.i'
'./.hg/store/data'
    d'finction'
    -'.hgignore.i'
    -'setup.py.i'
'./.hg/store/data/finction'
    -'util.py.i'
    -'cdg.py.i'
    -'finction.i'
    -'____init____.py.i'
Run Code Online (Sandbox Code Playgroud)

但是如果你坚持,os.path中有路径相关的工具,os.basename就是你正在看的东西.

>>> import os.path
>>> os.path.basename('/hello/world.h')
'world.h'
Run Code Online (Sandbox Code Playgroud)


Sma*_*ery 6

而不是使用'.' 作为您的目录,请参考其绝对路径:

for file in DirectoryWalker(os.path.abspath('.')):
    print file
Run Code Online (Sandbox Code Playgroud)

另外,我建议使用"文件"以外的单词,因为它意味着python语言中的某些东西.不是关键字,但它仍然运行.

顺便说一句,在处理文件名时,我发现os.path模块非常有用 - 我建议你仔细看看,特别是

os.path.normpath
Run Code Online (Sandbox Code Playgroud)

规范化路径(摆脱多余的'.'和'theFolderYouWereJustIn /../')

os.path.join
Run Code Online (Sandbox Code Playgroud)

加入两条路