【问题标题】:Python Create and edit file in each subdirectoryPython 在每个子目录中创建和编辑文件
【发布时间】:2013-11-27 23:28:00
【问题描述】:

我有一个包含子文件夹的主文件夹,每个文件夹都包含具有特定命名方案的文件。我已经对基于这些文件中的信息在单个目录中创建和编辑文本文档的函数进行了单元测试,但现在我遇到了试图让该函数遍历每个子目录的问题。

问题: 我收到第 38 行 if (row["r_id"]) in filters: 的“KeyError”。这是因为文件br_ids.csv 没有被创建。在单元测试中,这运行良好,所以我只能假设这是我使用os.walk 的一些问题。

代码:

import csv
import os

with open('hasf.txt','w') as hf:
    for root, subFolders, files in os.walk('/path/to/topdir/'):
#if folder contains 'f_r.csv', list the path in 'hasf.txt'
        if 'f_r.csv' in files:
            hf.write("%s\n" % root)

        if 'r.csv' in files:
            with open(os.path.join(root, "r.csv")) as inf, open(os.path.join(root, "br_ids.csv"), "w") as output:
                reader = csv.DictReader(inf, quotechar='"')
                headers = ["r_id"]
                writer_br = csv.DictWriter(output, headers, extrasaction='ignore')
                writer_br.writeheader()
                for row in reader:
                    if int(row["r_type"]) == 3:
                        writer_br.writerow(row)
        # End creating br_ids

        # parse the data you're about to filter with
            with open(os.path.join(root, 'br_ids.csv'), 'r') as f:
                filters = {(row["r_id"]) for row in csv.DictReader(f, delimiter=',', quotechar='"')}

            with open(os.path.join(root, 'bt_ids.csv'), 'w') as out_f:
                headers = ["t_id"]
                out = csv.DictWriter(out_f, headers, extrasaction='ignore')
                out.writeheader()

        # go thru your rows and see if the matching(row[r_id]) is
        # found in the previously parsed set of filters; if yes, skip the row 
                with open(os.path.join(root, 't.csv'), 'r') as f:
                    for row in csv.DictReader(f, delimiter=','):
                       if (row["r_id"]) in filters:
                            out.writerow(row)

我在这里遇到了一些类似的问题,但没有一个直接涉及在os.walk 的每个位置内创建、编辑和使用文件。这是我第一次使用 Python,我有些茫然。另外,如果有什么方法可以让我的其他代码更 Pythonic,我会全力以赴。

谢谢!

【问题讨论】:

  • 好吧,你是说 br_ids.csv 没有创建,这会导致错误,但如果这个文件没有创建,那么代码必须已经在with open(os.path.join(root, 'br_ids.csv'), 'r') as f: 行失败。

标签: python csv python-3.x os.walk


【解决方案1】:

事实证明,问题直接出在 KeyError 上——在某些文件夹中,br_id.csv 的条目为零,因此引发了 KeyError。我解决它的方法是使用try,如下所示:

    # parse the data you're about to filter with
        with open(os.path.join(root, 'br_ids.csv'), 'r') as f:
            filters = {(row["r_id"]) for row in csv.DictReader(f, delimiter=',', quotechar='"')}

        with open(os.path.join(root, 'bt_ids.csv'), 'w') as out_f:
            headers = ["t_id"]
            out = csv.DictWriter(out_f, headers, extrasaction='ignore')
            out.writeheader()

    # go thru your rows and see if the matching(row[r_id]) is
    # found in the previously parsed set of filters; if yes, skip the row 
            with open(os.path.join(root, 't.csv'), 'r') as f:
                for row in csv.DictReader(f, delimiter=','):
                    try:
                        if (row["r_id"]) in filters:
                            out.writerow(row)
                    except KeyError:
                        continue

在另一种情况下,我有一个if (row["r_id"]) not in filters:,并使用相同的方法绕过它,除了如果它返回一个KeyError,那么它继续执行out.writerow(row)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    相关资源
    最近更新 更多