• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            我希望你是我獨(dú)家記憶

            一段永遠(yuǎn)封存的記憶,隨風(fēng)而去
            posts - 263, comments - 31, trackbacks - 0, articles - 3
               :: 首頁(yè) :: 新隨筆 ::  :: 聚合  :: 管理

            os.walk()

            Posted on 2009-07-29 10:59 Hero 閱讀(1182) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): python

             

            Generate the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at directory top (including top itself), it 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 contain 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 argument 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 (perhaps using 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, impose a specific order of visiting, or even to inform walk() about directories the caller creates or renames before it resumes walk() again. Modifying dirnames when topdown is False is ineffective, because in bottom-up mode the directories in dirnames are generated before dirpath itself is generated.

            By default errors 
            from the listdir() call are ignored. If optional argument onerror is specified, it should be a function; it will be called with one argument, an OSError 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, walk() will 
            not walk down into symbolic links that resolve to directories. Set followlinks to True to visit directories pointed to by symlinks, on systems that support them.

            New 
            in version 2.6: The followlinks parameter.

            Note

            Be aware that setting followlinks to True can lead to infinite recursion 
            if a link points to a parent directory of itself. walk() does not keep track of the directories it visited already.

            Note

            If you 
            pass a relative pathname, don’t change the current working directory between resumptions of walk(). walk() never changes the current directory, and assumes that its caller doesn’t either.

            This example displays the number of bytes taken by non
            -directory files in each directory under the starting directory, except that it doesn’t look under any CVS subdirectory:

            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
            In the next example, walking the tree bottom-up is essential: rmdir() doesn’t allow deleting a directory before the directory is empty:

            # Delete everything reachable from the directory named in "top",
            #
             assuming there are no symbolic links.
            #
             CAUTION:  This is dangerous!  For example, if top == '/', it
            #
             could delete all your disk files.
            import os
            for root, dirs, files in os.walk(top, topdown=False):
                
            for name in files:
                    os.remove(os.path.join(root, name))
                
            for name in dirs:
                    os.rmdir(os.path.join(root, name))

             


            只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。
            相關(guān)文章:
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            亚洲午夜久久久久妓女影院| 精品国产91久久久久久久a| 久久精品国产99久久丝袜| 精品久久亚洲中文无码| 国产激情久久久久久熟女老人| 无码AV波多野结衣久久| 一本大道加勒比久久综合| 久久se这里只有精品| 久久亚洲国产精品成人AV秋霞| 无码人妻久久一区二区三区免费丨| 久久se精品一区精品二区| 欧美精品九九99久久在观看| 久久超乳爆乳中文字幕| 久久午夜福利电影| 久久精品水蜜桃av综合天堂 | 99久久久久| 色婷婷综合久久久久中文一区二区| 日本免费一区二区久久人人澡| 久久只有这精品99| 亚洲综合久久综合激情久久| 久久精品无码专区免费东京热| 久久久中文字幕日本| 精品一区二区久久| 久久久亚洲AV波多野结衣| 久久亚洲国产成人影院网站 | 久久精品成人影院| 久久精品视频网| 久久亚洲国产成人精品性色| 一本大道久久东京热无码AV| 精品久久久久久无码免费| 亚洲综合久久综合激情久久 | 久久久久久久免费视频| 狠狠久久综合| 国产精品美女久久久免费| 99久久99久久精品国产片| 91精品国产91久久久久福利| 久久99国产综合精品| 中文字幕无码免费久久| 亚洲精品国产第一综合99久久| 老司机午夜网站国内精品久久久久久久久 | 精产国品久久一二三产区区别 |