使用askdirectory比较Python中的.txt文件

时间:2017-03-29 21:20:31

标签: python python-2.7

我有以下代码来运行目录并选择所有文件并将它们与插入的wordlist文件进行比较。但是我收到以下错误TypeError: invalid file: ['C:/Users/Nathan/Desktop/chats\\(1,).out']我无法弄清楚如何更改os.path.join以正确显示文件位置。

    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 个答案:

答案 0 :(得分:1)

考虑这行代码:

with open(paths) as f:

问问自己,“什么是paths”?它是文件名的列表,而不是单个文件。这几乎是错误告诉你的:列表是无效文件。

考虑到你正在循环一个文件名列表,我的猜测是你的意图:

with open(file) as f:

或者

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