2017-03-29 7 views
0

私は以下のコードを使ってディレクトリを実行し、すべてのファイルを選択し、それらを挿入されたワードリストファイルと比較します。しかし、次のエラーが発生するTypeError: invalid file: ['C:/Users/Nathan/Desktop/chats\\(1,).out'] os.path.joinを変更してファイルの場所を正しく表示する方法を見つけられません。askdirectoryを使用してPythonの.txtファイルを比較する

self.wordopp = askdirectory(title="Select chat log directory") 
    path = self.wordopp 
    files = os.listdir(path) 
    paths = [] 
    wordlist = self.wordop 
    for file in files: 
     paths.append(os.path.join(path, file)) 
     f = open(wordlist) 
     l = set(w.strip().lower() for w in f) 
     with open(paths) as f: 
      found = False 
      file = open("out.txt", "w") 
      for line in paths: 
       line = line.lower() 
       if any(w in line for w in l): 
        found = True 
        file.write(line) 
        print(line) 
        if not found: 
         print(line) 

答えて

1

のコード行を考えてみましょう:

with open(paths) as f: 

は "pathsは何か"、自問してみてください? 1つのファイルではなく、ファイル名のリストです。これはエラーがあなたに伝えていることです:リストは無効なファイルです。

with open(file) as f: 

または多分

with open(paths[-1]) as f: 
を:

あなたはファイル名のリストをループしていることを考えると、私の推測では、あなたの意図が何をしているということです

関連する問題