zoukankan      html  css  js  c++  java
  • python_从文件查找关键字

    #1、写一个函数,传入一个路径和一个关键字(关键字是文件内容),找到文件内容里面有这个关键字的文件

    import os
    def find_file(keywords,path='/Users/wym/Desktop/work/Besttest/笔记/day6'):
    file_list = get_all_file(path)
    for file in file_list:
    if file.endswith('.py') or file.endswith('.txt'):
    with open(file,'r',encoding='utf-8') as fr:
    if keywords in fr.read():
    print(file)
    print('=='*20)
    def get_all_file(path):
    file_list = []
    for cur_path, cur_dirs, cur_files in os.walk(path):
    for name in cur_files:
    file_list.append(os.path.join(cur_path,name))
    return file_list
    keywords = input("Please input the key words that you want to search:")
    path = input("Please input the filepath:")
    find_file(keywords,path)

  • 相关阅读:
    LeetCode 104
    LeetCode 100
    LeetCode 27
    LeetCode 7
    LeetCode 8
    蘑菇街2017春招笔试
    codeforces 5D
    codeforces 5C
    codeforces 875B
    codeforces 876B
  • 原文地址:https://www.cnblogs.com/mercywym/p/10124518.html
Copyright © 2011-2022 走看看