File not found error, even though file was found

前端 未结 2 848
没有蜡笔的小新
没有蜡笔的小新 2021-01-26 20:55

Using the following bit of code:

for root, dirs, files in os.walk(corpus_name):
    for file in files:
        if file.endswith(\".v4_gold_conll\"):
                     


        
2条回答
  •  独厮守ぢ
    2021-01-26 21:38

    You'll have to join the root with the filename.

    for root, dirs, files in os.walk(corpus_name):
        for file in files:
            if file.endswith(".v4_gold_conll"):
                with open(os.path.join(root, file)) as f:
                tokens = [
                    line.split()[3]
                    for line in f
                    if line.strip() and not line.startswith("#")
                ]
                print(tokens)
    

提交回复
热议问题