Python 遍历所有文件夹及文件绝对路径

Python 2016-02-15 11:43 暂无评论

一般用到的是递归遍历 os.walk() 递归遍历的时候会有一些意外的问题,导致遍历出来的路径有错误。看一下帮助文档:

Help on function walk in module os:

walk(top, topdown=True, onerror=None, followlinks=False)
    Directory tree generator.

    For each directory in the directory tree rooted at top (including top
    itself, but excluding '.' and '..'), yields a 3-tuple

        dirpath, dirnames, filenames

    dirpath is a string, the path to the directory.  dirnames is a list of
    the names of the subdirectories in dirpath (excluding '.' and '..').
    filenames is a list of the names of the non-directory files in dirpath.
    Note that the names in the lists are just names, with no path components.
    To get a full path (which begins with top) to a file or directory in
    dirpath, do os.path.join(dirpath, name).

    If optional arg 'topdown' is true or not specified, the triple for a
    directory is generated before the triples for any of its subdirectories
    (directories are generated top down).  If topdown is false, the triple
    for a directory is generated after the triples for all of its
    subdirectories (directories are generated bottom up).

    When topdown is true, the caller can modify the dirnames list in-place
    (e.g., via del or slice assignment), and walk will only recurse into the
    subdirectories whose names remain in dirnames; this can be used to prune the
    search, or to impose a specific order of visiting.  Modifying dirnames when
    topdown is false is ineffective, since the directories in dirnames have
    already been generated by the time dirnames itself is generated. No matter
    the value of topdown, the list of subdirectories is retrieved before the
    tuples for the directory and its subdirectories are generated.

    By default errors from the os.listdir() call are ignored.  If
    optional arg 'onerror' is specified, it should be a function; it
    will be called with one argument, an os.error instance.  It can
    report the error to continue with the walk, or raise the exception
    to abort the walk.  Note that the filename is available as the
    filename attribute of the exception object.

    By default, os.walk does not follow symbolic links to subdirectories on
    systems that support them.  In order to get this functionality, set the
    optional argument 'followlinks' to true.

    Caution:  if you pass a relative pathname for top, don't change the
    current working directory between resumptions of walk.  walk never
    changes the current directory, and assumes that the client doesn't
    either.

    Example:

    import os
    from os.path import join, getsize
    for root, dirs, files in os.walk('python/Lib/email'):
        print root, "consumes",
        print sum([getsize(join(root, name)) for name in files]),
        print "bytes in", len(files), "non-directory files"
        if 'CVS' in dirs:
            dirs.remove('CVS')  # don't visit CVS directories

看一下例子,帮助文档里面页给了例子,这里是将目录遍历出来。

>> for parent, dirnames, filenames in os.walk(r'/Volumes/Files/PycharmProjects/ssbc'):
...     print parent
...
/Volumes/Files/PycharmProjects/ssbc
/Volumes/Files/PycharmProjects/ssbc/.git
/Volumes/Files/PycharmProjects/ssbc/.git/branches
/Volumes/Files/PycharmProjects/ssbc/.git/hooks
/Volumes/Files/PycharmProjects/ssbc/.git/info
/Volumes/Files/PycharmProjects/ssbc/.git/logs
/Volumes/Files/PycharmProjects/ssbc/.git/logs/refs
/Volumes/Files/PycharmProjects/ssbc/.git/logs/refs/heads
/Volumes/Files/PycharmProjects/ssbc/.git/logs/refs/remotes
/Volumes/Files/PycharmProjects/ssbc/.git/logs/refs/remotes/origin
/Volumes/Files/PycharmProjects/ssbc/.git/objects
/Volumes/Files/PycharmProjects/ssbc/.git/objects/info
/Volumes/Files/PycharmProjects/ssbc/.git/objects/pack
/Volumes/Files/PycharmProjects/ssbc/.git/refs
/Volumes/Files/PycharmProjects/ssbc/.git/refs/heads
/Volumes/Files/PycharmProjects/ssbc/.git/refs/remotes
/Volumes/Files/PycharmProjects/ssbc/.git/refs/remotes/origin
/Volumes/Files/PycharmProjects/ssbc/.git/refs/tags
/Volumes/Files/PycharmProjects/ssbc/lib
/Volumes/Files/PycharmProjects/ssbc/search
/Volumes/Files/PycharmProjects/ssbc/search/management
/Volumes/Files/PycharmProjects/ssbc/search/management/commands
/Volumes/Files/PycharmProjects/ssbc/search/migrations
/Volumes/Files/PycharmProjects/ssbc/ssbc
/Volumes/Files/PycharmProjects/ssbc/web
/Volumes/Files/PycharmProjects/ssbc/web/migrations
/Volumes/Files/PycharmProjects/ssbc/web/static
/Volumes/Files/PycharmProjects/ssbc/web/static/css
/Volumes/Files/PycharmProjects/ssbc/web/static/fonts
/Volumes/Files/PycharmProjects/ssbc/web/static/js
/Volumes/Files/PycharmProjects/ssbc/web/templates
/Volumes/Files/PycharmProjects/ssbc/web/templatetags
/Volumes/Files/PycharmProjects/ssbc/workers

将文件夹名称列出,第一个元素列表列出该文件夹中所有子文件,紧接着的元素列表均是该父目录下的子文件夹(有些拗口,就是父目录下的子列表,第一个元素列表中的父目录,然后就是子目录):

>>> for parent, dirnames, filenames in os.walk(r'/Volumes/Files/PycharmProjects/ssbc'):
...     print dirnames
...
['.git', 'lib', 'search', 'ssbc', 'web', 'workers']
['branches', 'hooks', 'info', 'logs', 'objects', 'refs']
[]
[]
[]
['refs']
['heads', 'remotes']
[]
['origin']
[]
['info', 'pack']
[]
[]
['heads', 'remotes', 'tags']
[]
['origin']
[]
[]
[]
['management', 'migrations']
['commands']
[]
[]
[]
['migrations', 'static', 'templates', 'templatetags']
[]
['css', 'fonts', 'js']
[]
[]
[]
[]
[]
[]

输出所有文件名称:

>>> for parent, dirnames, filenames in os.walk(r'/Volumes/Files/PycharmProjects/ssbc'):
...     print filenames
...
['.DS_Store', '.gitignore', 'LICENSE', 'manage.py', 'README.md', 'requirements.txt', 'sphinx.conf', 'super.conf']
['config', 'description', 'HEAD', 'index', 'packed-refs']
[]
['applypatch-msg.sample', 'commit-msg.sample', 'post-update.sample', 'pre-applypatch.sample', 'pre-commit.sample', 'pre-push.sample', 'pre-rebase.sample', 'prepare-commit-msg.sample', 'update.sample']
['exclude']
['HEAD']
[]
['master']
[]
['HEAD']
[]
[]
['pack-32574c66923f00c09b95225d031c3465720cde2b.idx', 'pack-32574c66923f00c09b95225d031c3465720cde2b.pack']
[]
['master']
[]
['HEAD']
[]
['__init__.py', 'politics.py', 'politics.txt']
['__init__.py', 'admin.py', 'models.py', 'tests.py', 'timermiddleware.py', 'urls.py', 'views.py']
['__init__.py']
['__init__.py', 'loadhash.py', 'loadlist.py', 'loadstatus.py']
['0001_initial.py', '0002_filelist_hash_statusreport.py', '0003_auto_20150511_0316.py', '0004_auto_20150511_0339.py', '0005_auto_20150721_0628.py', '__init__.py']
['__init__.py', 'settings.py', 'urls.py', 'wsgi.py']
['__init__.py', 'admin.py', 'models.py', 'tests.py', 'views.py']
['__init__.py']
['favicon.ico', 'gaoqing.png', 'howto1.png', 'ssbc.png']
['bootstrap-theme.css', 'bootstrap-theme.css.map', 'bootstrap-theme.min.css', 'bootstrap.css', 'bootstrap.css.map', 'bootstrap.min.css', 'ssbc.css']
['glyphicons-halflings-regular.eot', 'glyphicons-halflings-regular.svg', 'glyphicons-halflings-regular.ttf', 'glyphicons-halflings-regular.woff', 'glyphicons-halflings-regular.woff2']
['bootstrap-typeahead.min.js', 'bootstrap.js', 'bootstrap.min.js', 'npm.js', 'ssbc.js']
['analytics.html', 'base.html', 'foot.html', 'howto.html', 'index.html', 'info.html', 'layout.html', 'list.html', 'pagination.html']
['__init__.py', 'filters.py']
['__init__.py', 'bencode.py', 'index_worker.py', 'LICENSE', 'ltMetadata.py', 'metautils.py', 'simdht_worker.py', 'simMetadata.py']

输出所有文件路径:

>>> for parent, dirnames, filenames in os.walk(r'/Volumes/Files/PycharmProjects/ssbc'):
...     for i in filenames:
...         print os.path.join(parent,i)
...
/Volumes/Files/PycharmProjects/ssbc/.DS_Store
/Volumes/Files/PycharmProjects/ssbc/.gitignore
/Volumes/Files/PycharmProjects/ssbc/LICENSE
/Volumes/Files/PycharmProjects/ssbc/manage.py
/Volumes/Files/PycharmProjects/ssbc/README.md
/Volumes/Files/PycharmProjects/ssbc/requirements.txt
/Volumes/Files/PycharmProjects/ssbc/sphinx.conf
/Volumes/Files/PycharmProjects/ssbc/super.conf
/Volumes/Files/PycharmProjects/ssbc/.git/config
/Volumes/Files/PycharmProjects/ssbc/.git/description
/Volumes/Files/PycharmProjects/ssbc/.git/HEAD
/Volumes/Files/PycharmProjects/ssbc/.git/index
/Volumes/Files/PycharmProjects/ssbc/.git/packed-refs
/Volumes/Files/PycharmProjects/ssbc/.git/hooks/applypatch-msg.sample
/Volumes/Files/PycharmProjects/ssbc/.git/hooks/commit-msg.sample
/Volumes/Files/PycharmProjects/ssbc/.git/hooks/post-update.sample
/Volumes/Files/PycharmProjects/ssbc/.git/hooks/pre-applypatch.sample
/Volumes/Files/PycharmProjects/ssbc/.git/hooks/pre-commit.sample
/Volumes/Files/PycharmProjects/ssbc/.git/hooks/pre-push.sample
/Volumes/Files/PycharmProjects/ssbc/.git/hooks/pre-rebase.sample
/Volumes/Files/PycharmProjects/ssbc/.git/hooks/prepare-commit-msg.sample
/Volumes/Files/PycharmProjects/ssbc/.git/hooks/update.sample
/Volumes/Files/PycharmProjects/ssbc/.git/info/exclude
/Volumes/Files/PycharmProjects/ssbc/.git/logs/HEAD
/Volumes/Files/PycharmProjects/ssbc/.git/logs/refs/heads/master
/Volumes/Files/PycharmProjects/ssbc/.git/logs/refs/remotes/origin/HEAD
/Volumes/Files/PycharmProjects/ssbc/.git/objects/pack/pack-32574c66923f00c09b95225d031c3465720cde2b.idx
/Volumes/Files/PycharmProjects/ssbc/.git/objects/pack/pack-32574c66923f00c09b95225d031c3465720cde2b.pack
/Volumes/Files/PycharmProjects/ssbc/.git/refs/heads/master
/Volumes/Files/PycharmProjects/ssbc/.git/refs/remotes/origin/HEAD
/Volumes/Files/PycharmProjects/ssbc/lib/__init__.py
/Volumes/Files/PycharmProjects/ssbc/lib/politics.py
/Volumes/Files/PycharmProjects/ssbc/lib/politics.txt
/Volumes/Files/PycharmProjects/ssbc/search/__init__.py
/Volumes/Files/PycharmProjects/ssbc/search/admin.py
/Volumes/Files/PycharmProjects/ssbc/search/models.py
/Volumes/Files/PycharmProjects/ssbc/search/tests.py
/Volumes/Files/PycharmProjects/ssbc/search/timermiddleware.py
/Volumes/Files/PycharmProjects/ssbc/search/urls.py
/Volumes/Files/PycharmProjects/ssbc/search/views.py
/Volumes/Files/PycharmProjects/ssbc/search/management/__init__.py
/Volumes/Files/PycharmProjects/ssbc/search/management/commands/__init__.py
/Volumes/Files/PycharmProjects/ssbc/search/management/commands/loadhash.py
/Volumes/Files/PycharmProjects/ssbc/search/management/commands/loadlist.py
/Volumes/Files/PycharmProjects/ssbc/search/management/commands/loadstatus.py
/Volumes/Files/PycharmProjects/ssbc/search/migrations/0001_initial.py
/Volumes/Files/PycharmProjects/ssbc/search/migrations/0002_filelist_hash_statusreport.py
/Volumes/Files/PycharmProjects/ssbc/search/migrations/0003_auto_20150511_0316.py
/Volumes/Files/PycharmProjects/ssbc/search/migrations/0004_auto_20150511_0339.py
/Volumes/Files/PycharmProjects/ssbc/search/migrations/0005_auto_20150721_0628.py
/Volumes/Files/PycharmProjects/ssbc/search/migrations/__init__.py
/Volumes/Files/PycharmProjects/ssbc/ssbc/__init__.py
/Volumes/Files/PycharmProjects/ssbc/ssbc/settings.py
/Volumes/Files/PycharmProjects/ssbc/ssbc/urls.py
/Volumes/Files/PycharmProjects/ssbc/ssbc/wsgi.py
/Volumes/Files/PycharmProjects/ssbc/web/__init__.py
/Volumes/Files/PycharmProjects/ssbc/web/admin.py
/Volumes/Files/PycharmProjects/ssbc/web/models.py
/Volumes/Files/PycharmProjects/ssbc/web/tests.py
/Volumes/Files/PycharmProjects/ssbc/web/views.py
/Volumes/Files/PycharmProjects/ssbc/web/migrations/__init__.py
/Volumes/Files/PycharmProjects/ssbc/web/static/favicon.ico
/Volumes/Files/PycharmProjects/ssbc/web/static/gaoqing.png
/Volumes/Files/PycharmProjects/ssbc/web/static/howto1.png
/Volumes/Files/PycharmProjects/ssbc/web/static/ssbc.png
/Volumes/Files/PycharmProjects/ssbc/web/static/css/bootstrap-theme.css
/Volumes/Files/PycharmProjects/ssbc/web/static/css/bootstrap-theme.css.map
/Volumes/Files/PycharmProjects/ssbc/web/static/css/bootstrap-theme.min.css
/Volumes/Files/PycharmProjects/ssbc/web/static/css/bootstrap.css
/Volumes/Files/PycharmProjects/ssbc/web/static/css/bootstrap.css.map
/Volumes/Files/PycharmProjects/ssbc/web/static/css/bootstrap.min.css
/Volumes/Files/PycharmProjects/ssbc/web/static/css/ssbc.css
/Volumes/Files/PycharmProjects/ssbc/web/static/fonts/glyphicons-halflings-regular.eot
/Volumes/Files/PycharmProjects/ssbc/web/static/fonts/glyphicons-halflings-regular.svg
/Volumes/Files/PycharmProjects/ssbc/web/static/fonts/glyphicons-halflings-regular.ttf
/Volumes/Files/PycharmProjects/ssbc/web/static/fonts/glyphicons-halflings-regular.woff
/Volumes/Files/PycharmProjects/ssbc/web/static/fonts/glyphicons-halflings-regular.woff2
/Volumes/Files/PycharmProjects/ssbc/web/static/js/bootstrap-typeahead.min.js
/Volumes/Files/PycharmProjects/ssbc/web/static/js/bootstrap.js
/Volumes/Files/PycharmProjects/ssbc/web/static/js/bootstrap.min.js
/Volumes/Files/PycharmProjects/ssbc/web/static/js/npm.js
/Volumes/Files/PycharmProjects/ssbc/web/static/js/ssbc.js
/Volumes/Files/PycharmProjects/ssbc/web/templates/analytics.html
/Volumes/Files/PycharmProjects/ssbc/web/templates/base.html
/Volumes/Files/PycharmProjects/ssbc/web/templates/foot.html
/Volumes/Files/PycharmProjects/ssbc/web/templates/howto.html
/Volumes/Files/PycharmProjects/ssbc/web/templates/index.html
/Volumes/Files/PycharmProjects/ssbc/web/templates/info.html
/Volumes/Files/PycharmProjects/ssbc/web/templates/layout.html
/Volumes/Files/PycharmProjects/ssbc/web/templates/list.html
/Volumes/Files/PycharmProjects/ssbc/web/templates/pagination.html
/Volumes/Files/PycharmProjects/ssbc/web/templatetags/__init__.py
/Volumes/Files/PycharmProjects/ssbc/web/templatetags/filters.py
/Volumes/Files/PycharmProjects/ssbc/workers/__init__.py
/Volumes/Files/PycharmProjects/ssbc/workers/bencode.py
/Volumes/Files/PycharmProjects/ssbc/workers/index_worker.py
/Volumes/Files/PycharmProjects/ssbc/workers/LICENSE
/Volumes/Files/PycharmProjects/ssbc/workers/ltMetadata.py
/Volumes/Files/PycharmProjects/ssbc/workers/metautils.py
/Volumes/Files/PycharmProjects/ssbc/workers/simdht_worker.py
/Volumes/Files/PycharmProjects/ssbc/workers/simMetadata.py

在遍历的时候发现有过一个问题,就是在遍历的时候到最后一个目录的时候列出的路径最后一个文件夹的时候缺少了 / 仔细看看发现也并没有什么问题,难道是目录太深??也并没有,有时候一头雾水的没仔细看,有时候记起来了又忘记了(现在忘记了 :(

一般我们用到的就是遍历子文件夹及列出文件。够用了 =-=

暂无评论