在Python中:使用raw_input在多个文本中搜索两个单词

时间:2012-12-21 18:07:58

标签: python macos

我想在目录中打开(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)

0 个答案:

没有答案