繁体   English   中英

使用Askdirectory在Python中比较.txt文件

[英]Comparing .txt files in Python using askdirectory

我有以下代码可在目录中运行并选择所有文件,并将它们与插入的单词列表文件进行比较。 但是我收到以下错误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)

考虑以下代码行:

with open(paths) as f:

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

考虑到您正在遍历文件名列表,我想您的意图是:

with open(file) as f:

或者可能

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM