Python for循环遍历目录文件

标签 python loops python-2.7

我在两个不同的目录中有文件,其中包含索引文本的腌制列表,如下所示,以 .out 格式保存:

(lp0 S'TCCTCTTGGAGCACCAGCTAATATTTCATCAGTATTCGCTGAATCTTCGGACATAGTTCA' p1 aS'TTCGGACATAGTTCATTCATATTTATTTGCCCAATACCCGCACGAAGAAGCCTTGCAGAC' p2 as'AGAAGCCTTGCAGACACCGTGGCA' p3 a.

我想要完成的任务是从可疑文本目录中打开一个文件,并将其与源文本目录中的每个文件进行比较,使用 python 的 difflib,打印出一个数字指示它们是否匹配,然后执行与可疑文本目录中的其余文件相同。 (旁注:如果有人知道比较两个索引文本列表的更详细方法,我洗耳恭听,但这远不是优先事项)

我当前的问题是使用 for 循环来完成此任务,它不起作用。我的意思是,我可以循环浏览文件夹,它们可以打印出文件夹名称,但文件本身的内容不会改变。该循环目前仅多次比较每个目录中的一个文件,我不知道如何修复它。

欢迎提出任何建议,如果我的解释足够清楚,请随时提出任何问题。

谢谢。另外,我知道这是一个常见问题,我已尽力查看以前的答案并应用他们使用的内容,但我很难这样做,因为我不太擅长编程。

提前致谢!

F

代码如下:

import string
import pickle
import sys
import glob
import difflib


sourcePath = 'C:\Users\User\Sou2/*.out'
suspectPath = 'C:\Users\User\Susp2/*.out'
list_of_source_files = glob.glob(sourcePath)
list_of_suspect_files = glob.glob(suspectPath)


def get_source_files(list_of_source_files):

    for source_file_name in list_of_source_files:
        with open(source_file_name) as source_file:
            sourceText = pickle.load(source_file)
        return sourceText


get_suspect_files(list_of_suspect_files):

    for suspect_file_name in list_of_suspect_files:
        with open(suspect_file_name) as suspect_file:
            suspectText = pickle.load(suspect_file)
        return suspectText


def matching(sourceText,suspectText):

            matching = difflib.SequenceMatcher(None,sourceText,suspectText)
            print matching.ratio()


def main():

    for suspectItem in list_of_suspect_files:
        suspectText = get_suspect_files(list_of_suspect_files)
        print ('----------------SEPERATOR-----------------')
        for sourceItem in list_of_source_files:
            sourceText = get_source_files(list_of_source_files)
            matching(sourceText,suspectText)


main()

当前结果:

----------------SEPERATOR-----------------
0.0
0.0
0.0
----------------SEPERATOR-----------------
0.0
0.0
0.0
----------------SEPERATOR-----------------
0.0
0.0
0.0
----------------SEPERATOR-----------------
0.0
0.0
0.0

对于其中一些来说,这应该是 1.0,因为我故意将匹配的索引文本添加到系统文本中。

最佳答案

您的函数 get_source_filesget_suspect_files 每个都包含循环,但在循环的第一次迭代时返回。这就是为什么您的程序只查看每个列表中的第一个文件。

此外,这两个函数中的循环与主函数中的循环重复。在您的主函数中,您永远不会使用循环变量 suspectItemsourceItem,因此这些循环只是执行相同的操作多次。

可能,您混淆了 yieldreturn,并以某种方式期望您的函数表现得像生成器。

这样的东西应该有效

def get_text(file_name):
    with open(file_name) as file:
        return pickle.load(file)

def matching(sourceText,suspectText):
    matching = difflib.SequenceMatcher(None,sourceText,suspectText)
    print matching.ratio()

def main():
    for suspect_file in list_of_suspect_files:
        print ('----------------SEPERATOR-----------------')
        suspect_text = get_text(suspect_file)
        for source_file in list_of_source_files:
            source_text = get_text(source_file)
            matching(source_text, suspect_text)

main()

请注意,这会在每次迭代中重复加载源文本。如果速度很慢,并且文本不太长而无法放入内存,您可以将所有源文本和可疑文本存储在列表中。

关于Python for循环遍历目录文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23788367/

相关文章:

python - 如何从 Route() 装饰器导入变量以形成 Flask 中的文件?

c# - 128位整数的指南

python - 映射而不是for循环

python - Pylint的R0902是什么意思?为什么我们有这个限制?

python - 使用 BS4、Selenium 在 Python 中抓取动态数据并避免重复

c++ - 在数组和循环方面需要帮助

python - 使用 boto3 清空 s3 存储桶的最快方法是什么?

python - 将多列求和为两列输出/数据帧

python - 服务器端 COLLADA 转换器

python - 嵌套循环未获得所需的输出