【问题标题】:Python: why is my method using os.walk does not return all available paths?Python:为什么我使用 os.walk 的方法没有返回所有可用路径?
【发布时间】:2013-09-15 13:15:36
【问题描述】:

问题如题,方法如下:

 def walkThroughPath(self , sBasePath, blFolders = True, blFiles = True ):
      aPaths = []
      for sRootDir, aSubFolders, aFiles in os.walk( sBasePath ):
           for sFolder in aSubFolders:
                if blFolders == True:
                     aPaths.append( sRootDir )
                for sFileName in aFiles:
                     if blFiles == True:
                          aPaths.append( sRootDir + "/" + sFileName )

      return aPaths

该方法返回大量子文件夹和文件,但肯定不是我找到的全部。

我的方法有什么问题(或者是 os.walk 的错误用法)?

对于那些对背景感兴趣的人:

http://www.playonlinux.com/en/topic-10962-centralized_wineprefix_as_preparation_for_debpackages.html

【问题讨论】:

  • 如果你要使用匈牙利符号do it right。更好的是改善名称。复数表示集合,动词+名词表示布尔等。另外,您的第三个 for 循环是否缩进正确?
  • 嗨史蒂文,你是对的。我什至不知道匈牙利符号。它只是我习惯用 PHP 编码的方式,我只是将它移植到 python,即使我知道 python 使用其他 labeks 作为他们的类型(当然还有其他的):-)。翻译:s=string,a=array,bl=boolean。很明显,这对于使用此类标准的人来说并不明显。希望代码无论如何都是可以理解的;-)

标签: python path subdirectory os.walk


【解决方案1】:

这里有两种可能性:

浏览了您提供的链接,看起来followlinks=True 可能是解决方案。

【讨论】:

    【解决方案2】:

    您的两个提示都带来了现在看起来像这样的最终解决方案:

    def walkThroughPath(self , sBasePath, blFolders = True, blFiles = True, blFollowSymlinks = True  ):
        aPaths = []
        for sRootDir, aSubFolders, aFiles in os.walk( sBasePath, blFollowSymlinks ):
            for sFolder in aSubFolders:
                if blFolders == True:
                    try:
                        aPaths.index( sRootDir )
                        blPathExists = True
                    except:
                        blPathExists = False
                        pass
    
                    if blPathExists == False:
                        aPaths.append( sRootDir )
                        self.logDebug("Append: " + sRootDir )
                        self.logDebug("Current content of aPaths: \n" + pprint.pformat(aPaths) )
    
            for sFileName in aFiles:
                self.logDebug("Current root dir: " + sRootDir )
                if blFiles == True:
                    try:
                        aPaths.index( sRootDir + "/" + sFileName )
                        blPathExists = True
                    except:
                        blPathExists = False
                        pass
    
                    if  blPathExists == False:
                        aPaths.append( sRootDir + "/" + sFileName )
                if blFolders == True:
                    try:
                        aPaths.index( sRootDir )
                        blPathExists = True
                    except:
                        blPathExists = False
                        pass
    
                    if blPathExists == False:
                        aPaths.append( sRootDir )
                        self.logDebug("Append: " + sRootDir )
                        self.logDebug("Current content of aPaths: \n" + pprint.pformat(aPaths) )
    
        self.logDebug("Folders: " + str(blFolders) )    
        self.logDebug("Files  : " + str(blFiles) )  
        self.logDebug("Paths found in " + sBasePath + " : \n" + pprint.pformat(aPaths) )    
        return aPaths
    

    首先,正如 Steven 所说,我缩进不正确。

    os.walk 似乎不像我预期的那样处理列表。文件的文件夹不一定会出现在文件夹列表中。这导致我遗漏了许多文件夹,因为这些文件夹路径已在文件列表中。此外,我只检查了这个有限文件夹列表中的文件。

    接下来,我按照 unutbu 的建议添加了可选的跟随符号链接标志。也许在我的情况下,它们最终也会被需要。

    上面的那些方法肯定是一个改进的候选者,但它至少可以工作:-)

    最好的,

    安德烈

    【讨论】:

      【解决方案3】:

      我知道你已经解决了这个问题。但对于进一步的参考:如果您想以 32 bit application64 bit Windows 上运行的身份遍历目录,请确保检查重定向的目录。

      %windir%\System32 目录是为 64 位应用程序保留的。大多数 DLL 文件名在创建 64 位版本的 DLL 时没有更改,因此 32 位版本的 DLL 存储在不同的目录中。 WOW64 通过使用文件系统重定向器隐藏了这种差异。

      File System Redirector on MSDN

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-03
        • 1970-01-01
        • 2019-03-08
        相关资源
        最近更新 更多