我有一個主文件夾用子文件夾填充,並且每個文件夾都包含具有特定命名方案的文件。基於這些文件中的信息,我已經單元測試了一個用於在單個目錄中創建和編輯文本文檔的功能,但是現在我遇到了試圖讓此函數遍歷每個子目錄的問題。Python在每個子目錄中創建和編輯文件
問題: 我得到第38行的「KeyError」if (row["r_id"]) in filters:
。這是因爲文件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-line(os.path.join根,'br_ids.csv'),'r')作爲f:'。 – Kritzefitz