简体   繁体   English

如何准确地搜索包含文本文件中元素的目录?

[英]How can I accurately search a directory with elements from a text file?

Fairly newish to python.对 python 来说相当新。 I'm trying to use a list made from a text file to search directories for files and folders.我正在尝试使用由文本文件组成的列表来搜索文件和文件夹的目录。 Its working to certain extent but only if the search element is at the start of the file/folder name as I'm using .startswith().它在一定程度上起作用,但前提是搜索元素位于文件/文件夹名称的开头,因为我正在使用 .startswith()。

The code I'm using is below.我正在使用的代码如下。

I'm thinking regex might be the way to go but can't seem to figure it out.我认为正则表达式可能是要走的路,但似乎无法弄清楚。

Any help appreciated.任何帮助表示赞赏。

Thanks.谢谢。

import os

def data_cleansing(path):

    dirscount = 0
    filescount = 0


    with open("Y:\Admin\Data Cleansing\DCList.txt","r") as f:
        x = f.readlines()
        x = [x.replace("\n","") for x in x]

    #print(x)


    for root, dirs, files in os.walk(path):
        for dirs in dirs:
            if dirs.startswith(tuple(x)):
                dirscount = dirscount + 1
                #print(dirscount)
                print(os.path.join(dirs))



    for root, dirs, files in os.walk(path):
        for files in files:
            if files.startswith(tuple(x)):
                filescount = filescount + 1
                #print(filescount)
                print(os.path.join(files))



    total = (dirscount + filescount)
    print(total,"files and folders found in",path,"that need cleansing.")

data_cleansing(r"G:\Processed\failed")
print("*"*70)
data_cleansing(r"G:\Processed\done")
print("*"*70)
data_cleansing(r"S:\Prosort")
print("*"*70)
data_cleansing(r"S:\Prosort_Archive")

Does this fit your needs?这是否符合您的需求?

from os import scandir
find_list = ['Uni', '.doc', 'XML']
path = r"c:\\Autoit\\ForumTests\\"

def scantree(path):
    """Recursively yield DirEntry objects for given directory."""
    for entry in scandir(path):
        if entry.is_dir(follow_symlinks=False):
            yield from scantree(entry.path)
        else:
            yield entry

if __name__ == '__main__':
    for entry in scantree(path):
        for pattern in find_list:
            if pattern in entry.path:
                print(pattern, '-->', entry.path)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何操作列表中文本文件中的元素? - How can I manipulate elements from text file in a list? 如何在Python中从导入的文本文件中打印元素? - How can I print elements from an imported text file in Python? 如何在python中搜索并获取DLL文件的目录 - How can I search and get the directory of a DLL file in python 如何在python 3.5.2中使用write函数并搜索我想要准确的文件? - How to use the write function in python 3.5.2 and search for the file I want to accurately? 如何在完成文本替换后准确设置新光标位置 - How can I accurately set the new cursor positions after text replacements have been made 如何在目录中的所有文本文件中搜索字符串并将找到的结果放在 Python 的文本文件中 - How do I search all text files in directory for a string and place the found result in a text file in Python 如何从用户输入中搜索文本文件中的单词列表? - How can I search a text file for a list of words from user input? 如何从用户输入中搜索单词列表的文本文件,并打印包含这些单词的行? - how can i search a text file of list of words from user input and print the line which contains these words? 使用重新和匹配,如何从文本文件中搜索和获取某些数据? - Using re and matching, how can I search and get certain data from a text file? 如何在文本文件中搜索特定列以获取用户输入 - How can I search a specific column in text file for a user input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM