Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在文件列表(当前目录)中搜索单词并返回包含单词python的所有文件_Python_Python 3.x_Search - Fatal编程技术网

在文件列表(当前目录)中搜索单词并返回包含单词python的所有文件

在文件列表(当前目录)中搜索单词并返回包含单词python的所有文件,python,python-3.x,search,Python,Python 3.x,Search,有人能告诉我哪里出了问题吗?elif-search-in-words:如果使用search-in-words,则无需将文件内容拆分为多行。即,如果在open(files.read().lower()中搜索:好的,我是新手,我有一个非常大的文件,比如12GB,我不想把它全部加载到内存中。逐行搜索就可以了。elif-search-in-words:如果你使用search-in-words那么就没有必要把文件的内容分成几行。也就是说,If-search-in-open(files).read().lo

有人能告诉我哪里出了问题吗?

elif-search-in-words:
如果使用
search-in-words
,则无需将文件内容拆分为多行。即,
如果在open(files.read().lower()中搜索:
好的,我是新手,我有一个非常大的文件,比如12GB,我不想把它全部加载到内存中。逐行搜索就可以了。
elif-search-in-words:
如果你使用
search-in-words
那么就没有必要把文件的内容分成几行。也就是说,
If-search-in-open(files).read().lower():
好的,我是新手,我有一个非常大的文件,比如12GB,我不想把它全部加载到内存中。一行一行应该可以。嗨,我对lambda函数一无所知,你能写基本的还是解释一下?ShikharSaxena我添加了基本版本。嗨,我对lambda函数一无所知,你能写基本的还是解释一下?ShikharSaxena我添加了基本版本。
import os, sys
text = [file_search for file_search in os.listdir() if file_search.endswith('.txt') or file_search.endswith('.py')]
search = input()
print (text)
for files in text:
    lines_list = open(files).read().lower().splitlines()
    bool1 = False
    for words in lines_list:
        if bool1 == True:
            break
        elif words == search:
            print (files)
            bool1 = True
        else:
            bool1 = False
#!/usrbin/python3
# 2017.11.20 15:31:09 CST
import os, sys
ext = ".txt"
fnames = [fname for fname in os.listdir() if fname.endswith(ext)]
word = input("Input the search word: ") 
res1 = list(filter(lambda fname: word in open(fname).read(), fnames))
res2 = list(filter(lambda fname: word.lower() in open(fname).read().lower(), fnames))
print("result1:", res1)
print("result2:", res2)

## 2017.11.20 18:54:10 CST
## The basic version
res = []
for fname in fnames:
    #if word in open(fname).read():
    if word.lower() in open(fname).read().lower():
        res.append(fname)

print("result:", res)