簡體   English   中英

如何使用 os.walk() 使用 for 循環在多個目錄中循環

[英]How to loop in multiple directories with a for loop using os.walk()

我創建了一個腳本,它應該在不同的目錄中循環,然后在初始目錄的子目錄中循環並找到要處理的文件。 圖形是這樣的:

1. Embeddings_test  # general directory
   > sm  # first level of directory
     > or # second level of directory
         > file 1
         > file 2

     > aug
         > file 1
         > file 2

     > or_et_aug
         > file 1
         > file 2

   > bc  # first level of directory
     > or # second level of directory
         > file 1
         > file 2

     > aug
         > file 1
         > file 2

     > or_et_aug
         > file 1
         > file 2

....
     

我想要做的是在每個子目錄中循環並檢索里面的文件,但不知何故,循環在第一個子目錄處停止:“sm”和“or”並且不要在“aug”的其他子目錄中循環和“or_et_aug”。

結果:

----------------
------------------Directory : sm ---- or
------------------
['flaubert-small-cased_emb_corpus_or_test.pkl', 'flaubert-small-cased_ylabels_corpus_or_test.pkl']
---file for emb--- :  flaubert-small-cased_emb_corpus_or_test.pkl
---file for labels--- :  flaubert-small-cased_ylabels_corpus_or_test.pkl
10 fold cross validation in processed ----------

Model name: Model_SVC_ovr
------------cross val predict used----------------
---------------cross val score used -----------------------
[1.   1.   1.   1.   1.   1.   1.   1.   0.75 1.  ]
0.97 accuracy with a standard deviation of 0.07
-------------------------------
-------------------------------
------------------
------------------Directory : bc ---- aug
------------------
------------------
------------------Directory : unc ---- or_et_aug
------------------

代碼行

# Loading features and classes

mod = ['sm', 'bc', 'unc', 'lg']

corpus = ['or','aug','or_et_aug']

for m, c in zip(mod, corpus):
    print("------------------")
    print("------------------Directory :", m, "----", c)
    print("------------------")
    for root, subdirs, files in os.walk("/ho/get/kelo/eXP/Test/embeddings_test" + "/" + m + "/" + c):
        #print(root)
        print(files)
        print("---file for emb--- : ", files[0])
        with open(os.path.join(root, files[0]), 'rb') as f1:
            features = pickle.load(f1)
        print("---file for labels--- : ", files[1])
        with open(os.path.join(root, files[1]), 'rb') as f2:
            ylabels = pickle.load(f2)
        # cross validation training and testing

        print("10 fold cross validation in processed ----------", "\n")
        models_list = classifiers()
        for model_name, model in models_list.items():
            print("Model name: {}".format(model_name))

            print("------------cross val predict used----------------")
            # cross val score : Run cross-validation for single metric evaluation

            print("---------------cross val score used -----------------------")
            scores = cross_val_score(model, features, ylabels, scoring='accuracy', cv=cv_splitter)
            print(scores)
            print("%0.2f accuracy with a standard deviation of %0.2f" % (scores.mean(), scores.std()))
            print("-------------------------------")
            print("-------------------------------")
            print("-------------------------------")
            print("-------------------------------")
            print("-------------------------------")

我希望打印每個目錄的結果,然后是子目錄。

您確定zip是您需要的 function 嗎?

import pathlib as pth
import itertools

ROOTDIR = pth.Path("/ho/get/kelo/eXP/Test/embeddings_test")

mod = ['sm', 'bc', 'unc', 'lg']
corpus = ['or','aug','or_et_aug']

for m, c in itertools.product(mod, corpus):
    root = ROOTDIR / m / c
    files = [file for file in root.iterdir() if file.is_file()]
    print(root)
    print(files)

稍后我將描述我的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM