【问题标题】:Finding all subfolders that contain two files that end with certain strings查找包含两个以特定字符串结尾的文件的所有子文件夹
【发布时间】:2017-08-10 15:44:26
【问题描述】:

所以我有一个文件夹,比如 D:\Tree,它只包含子文件夹(名称可能包含空格)。这些子文件夹包含一些文件 - 它们可能包含 "D:\Tree\SubfolderName\SubfolderName_One.txt""D:\Tree\SubfolderName\SubfolderName_Two.txt" 形式的文件(换句话说,子文件夹可能同时包含它们,一个或一个都不包含)。我需要找到每个子文件夹包含这两个文件的位置,并将它们的绝对路径发送到文本文件(以下面示例中解释的格式)。考虑 D:\Tree 中的这三个子文件夹:

D:\Tree\Grass contains Grass_One.txt and Grass_Two.txt
D:\Tree\Leaf contains Leaf_One.txt
D:\Tree\Branch contains Branch_One.txt and Branch_Two.txt

鉴于这种结构和上面提到的问题,我希望能够在 myfile.txt 中编写以下几行:

D:\Tree\Grass\Grass_One.txt D:\Tree\Grass\Grass_Two.txt
D:\Tree\Branch\Branch_One.txt D:\Tree\Branch\Branch_Two.txt

如何做到这一点?提前感谢您的帮助!

注意:在 myfile.txt 中,“file_One.txt”位于“file_Two.txt”之前,这一点非常重要

【问题讨论】:

  • 我考虑过的一件事是使用 "dir D:\Tree*_One.txt /b /s > somefile.txt" 和另一个列表 "dir D:\Tree* _Two.txt /b /s > somefile2.txt" 使用cmd,但是我不确定该怎么做。
  • 由于问题被标记为“python”,请添加您的代码以查看问题所在。
  • 我的建议是看看os.walk,尝试一下,如果遇到困难,再问一个更具体的问题。人们需要知道你实际做了什么以及失败的原因,而不是你考虑过的事情。

标签: python python-3.x


【解决方案1】:
import os

folderPath = r'Your Folder Path'

for (dirPath, allDirNames, allFileNames) in os.walk(folderPath):
    for fileName in allFileNames: 
        if fileName.endswith("One.txt") or fileName.endswith("Two.txt") :
            print (os.path.join(dirPath, fileName)) 
            # Or do your task as writing in file as per your need

希望这会有所帮助....

【讨论】:

    【解决方案2】:

    这是一个递归解决方案

    def findFiles(writable, current_path, ending1, ending2):
        '''
        :param writable:    file to write output to
        :param current_path: current path of recursive traversal of sub folders
        :param postfix:     the postfix which needs to match before
        :return: None
        '''
    
        # check if current path is a folder or not
        try:
            flist = os.listdir(current_path)
        except NotADirectoryError:
            return
    
    
        # stores files which match given endings
        ending1_files = []
        ending2_files = []
    
    
        for dirname  in flist:
            if dirname.endswith(ending1):
                ending1_files.append(dirname)
            elif dirname.endswith(ending2):
                ending2_files.append(dirname)
    
            findFiles(writable, current_path+ '/' + dirname, ending1, ending2)
    
        # see if exactly 2 files have matching the endings
        if len(ending1_files)  == 1 and len(ending2_files) == 1:
            writable.write(current_path+ '/'+ ending1_files[0] + ' ')
            writable.write(current_path + '/'+ ending2_files[0] + '\n')
    
    
    findFiles(sys.stdout, 'G:/testf', 'one.txt', 'two.txt')
    

    【讨论】:

    • 请原谅我的不专业,但究竟什么是后缀,在这种情况下如何定义它?从逻辑上看,它似乎是我正在寻找的文件末尾的内容(所以我猜是 _One.txt),但是我如何告诉这个脚本两个可能的结尾是什么?
    • 我改进了解决方案,现在它通过两个结尾并打印匹配它们的文件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    相关资源
    最近更新 更多