2012-07-16 110 views
0

我有类似以下的目录结构:剔除所有,但一个子目录从文件搜索

Dir1 
Dir2 
Dir3 
Dir4 
    L SubDir4.1 
    L SubDir4.2 
    L SubDir4.3 

我要生成的文件列表(带完整路径),其包括Dirs1-3所有内容,但Dir4内只有SubDir4.2。我到目前为止的代码是

import fnmatch 
import os 

for root, dirs, files in os.walk('.') 
    if 'Dir4' in dirs: 
     if not 'SubDir4.2' in 'Dir4': 
      dirs.remove('Dir4') 
    for file in files 
     print os.path.join(root, file) 

我的问题是,我试图排除没有SubDir4.2在它的路径是在Dir4排除一切,包括我想留的东西的任何文件中的一部分。我应该如何修改上面的内容来做我的愿望?

更新1:我应该补充说,在Dir4以下有很多目录,因此手动将它们列在排除列表中不是一个实际的选项。我希望能够指定SubDur4.2作为要读取的Dir4中唯一的子目录。

更新2:由于我不能控制的原因,我只能访问Python版本2.4.3。

+0

我很困惑。我可能看错了,但你说你只想要SubDir4。2在Dir4中,那么你说这个代码是排除你想要的Dir4中的东西。除了SubDir4.2的内容,Dir4中还有其他的东西吗? – JerseyMike 2012-07-16 12:19:56

+0

对不起,我感到困惑。我想排除'Dir4' **中的所有内容,但**'SubDir4.2'除外,但是我写的代码将'Dir4'中的所有内容排除在外,包括'SubDir4.2',我想知道如何修复它使前者成为可能。 – 2012-07-16 12:30:47

+0

没问题。只是想确保我明白。我提交的解决方案与您正在尝试完成的内容相匹配。我的大脑还没有接受“Pythonic Way”,所以MarcO的解决方案很难让我阅读,但我喜欢它。 :) – JerseyMike 2012-07-16 13:49:34

回答

1

你的代码段中有几个拼写错误。我建议这样的:

import os 

def any_p(iterable): 
    for element in iterable: 
     if element: 
      return True 
    return False 

include_dirs = ['Dir4/SubDir4.2', 'Dir1/SubDir4.2', 'Dir3', 'Dir2'] # List all your included folder names in that 


for root, dirs, files in os.walk('.'): 
    dirs[:] = [d for d in dirs if any_p(d in os.path.join(root, q_inc) for q_inc in include_dirs)] 

    for file in files: 
     print file 

编辑:根据意见,我已经改变了,所以这是包括列表,而不是排除一个。

EDIT2:添加了any_p

EDIT3bis(任何()为Python版本< 2.5等效功能):如果你有其他的子文件夹名称相同的其他文件夹中“SubDir4.2”,你可以使用下面的指定位置:

include_dirs = ['Dir4/SubDir4.2', 'Dir1/SubDir4.2'] 

假设你有一个Dir1/SubDir4.2。

如果他们是很多这些,那么你可能想要用fnmatch或可能是一个正则表达式来优化这种方法。

+0

非常感谢您的回复,但是我没有在我的问题中提及'Dir4'下面有很多子目录,所以手动列出它们并不是一个实际的解决方案。 – 2012-07-16 12:16:47

+0

再次感谢,但事实证明,我只能访问Python 2.4.3(工作计算机),并且任何()都没有被引入,直到2.5。 – 2012-07-16 12:37:19

+0

任何函数来自http://docs.python.org/library/functions.html#any – 2012-07-16 12:43:44

-1
for root, dirs, files in os.walk('.'): 
    tmp = root.split(os.path.sep) 
    if len(tmp)>2 and tmp[-2]=="Dir4" and tmp[-1]=="SubDir4.2": 
     continue 

    for file in files: 
     print os.path.join(root, file) 
+0

添加更多描述 – 2012-07-16 12:45:03

0

这里是一个脚本,执行此:

find . -name ".thumbnails" -prune -o -name ".eclipse" -prune -o -print 

的逻辑是这样:如果所述第一条件是真(ID EST中,dir是“缩略图”),它停止,并传递到下一个目录。如果它是错误的,它会检查considtion'.eclipse',等等。最后(如果所有这些条件都是错误的),它将打印当前文件。

这是shell,而不是python。

+0

感谢您的回复,但我需要这是python。 – 2012-07-16 12:17:07

0

我改变mstud的解决方案给你你在找什么:

import os; 

for root, dirs, files in os.walk('.'): 
    # Split the root into its path parts 
    tmp = root.split(os.path.sep) 
    # If the lenth of the path is long enough to be your path AND 
    # The second to last part of the path is Dir4 AND 
    # The last part of the path is SubDir4.2 THEN 
    # Stop processing this pass. 
    if (len(tmp) > 2) and (tmp[-2] == 'Dir4') and (tmp[-1] != 'SubDir4.2'): 
     continue 
    # If we aren't in Dir4, print the file paths. 
    if tmp[-1] != 'Dir4': 
     for file in files: 
      print os.path.join(root, file) 

总之,第一个“如果”跳过的Dir4下的任何目录的内容不在SubDir4.2打印。第二个“if”跳过打印Dir4目录的内容。

相关问题