我想在一个目录中打开(raw_input)多个文本文件,给它们一个名字(文档1,文档2...),使用“OR”搜索(raw_input)两个词,然后输入搜索词(不带字符“ .,/"" 只有小写的单词)和包含列表或新文本文件中的单词/单词的文件的名称:
我试图将文件放入字典中,但我真的不知道这是否是一个愚蠢的想法?
我不知道如何让用户同时在所有文件中搜索一两个单词(通过 raw_input)。你能帮我或给我一个提示吗?
我希望它打印如下内容:
SearchWord "在"文档 1,python.txt 中找到 SearchWord"在"文档 3,foobar.txt 中找到
import re, os
path = raw_input("insert path to directory :")
ex_library = os.listdir(path)
search_words = open("sword.txt", "w") # File or maybe list to put in the results
thelist = []
for texts in ex_library:
file = os.path.join(path, texts)
text = open(file, "r")
textname = os.path.basename(texts)
print textname
for names in textname.split():
thelist.append(names)
text.close()
print thelist
print "These texts are now open"
print "###########################"
count = 0
for y in ex_library:
count = count + 1
print count
print "texts total"
d ={}
for x in range(count):
d["Document {0}".format(x)] = None # I would like the values to be the names of the texts (text1.txt)