1

问题如标题,方法如下:

 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

4

3 回答 3

1

这里有两种可能性:

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

于 2013-09-15T13:25:18.533 回答
0

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

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

首先,正如史蒂文所说,我缩进不正确。

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

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

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

最好的,

安德烈

于 2013-09-21T18:34:34.483 回答
0

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

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

MSDN 上的文件系统重定向器

于 2017-04-26T15:50:51.487 回答