偷来的代码来学习

'''import os
def findfile(key_word):
    all_file = os.listdir(os.curdir)
    wordlist = []


    count = 0
    length = len(key_word)
    line = 1
    countlist = []
    countline = []

    for each_file in all_file:
        if each_file is os.path.isdir:
            os.chdir(each_file)
            findfile(key_word)
            os.chdir(os.pardir)

        elif os.path.splitext(each_file)[1] == '.txt':
            f = open(each_file)


            for each_word in f.read():
                count += 1
                wordlist.append(each_word)
                if each_word == '\n':
                    line += 1
                    count = 0
                    wordlist = []  #记得把列表清空

                elif ('').join(wordlist[count-1: count-1+length]) == key_word:
                    countlist.append(count)
                    countline.append(line)

                    print('在文件【%s%s%s】中找到关键字【%s】' % (os.curdir, os.sep, each_file, key_word))
                    print('关键字出现在第【%s】行,第【%s】个位置' % (countline, countlist))


            f.close()



key_word = input('请将该脚本放于待查找的文件夹内,请输入关键字: ')
findfile(key_word)
'''

import os

def print_pos(key_dict):
    keys = key_dict.keys()  #分别用字典来表示行与位置
    keys = sorted(keys)   #由于字典是无序的,我们这里对行数进行排序

    for each_key in keys:
        print('关键字出现在第 %s 行,第%s个位置。' % (each_key, str(key_dict[each_key])))

def pos_in_line(line, key):
    pos = []
    begin = line.find(key)
    while begin != -1:
        pos.append(begin + 1) #用户的角度是从1开始数
        begin = line.find(key, begin+1) #从下一个位置继续查找

    return pos

def search_in_file(file_name, key):
    f = open(file_name)
    count = 0 #记录行数
    key_dict = dict() #字典,用户存放key所在具体行数对应具体位置

    for each_line in f:
        count += 1
        if key in each_line:
            pos = pos_in_line(each_line, key) #key在每行对应的位置
            key_dict[count] = pos
    f.close()
    return key_dict

def search_files(key, detail):
    all_files = os.walk(os.getcwd())  #遍历该目录,返回三元组,(路径,[包含目录],[包含文件])
    txt_files = []

    for i in all_files:
        for each_file in i[2]:

            if os.path.splitext(each_file)[1] == '.txt':
                each_file = os.path.join(i[0], each_file)   #将目录和文件名合并成一个完整的目录名
                txt_files.append(each_file)    #textfile集合收集后缀为目的后缀的文件


    for each_txt_file in txt_files:
        key_dict = search_in_file(each_txt_file, key)
        if key_dict:
            print('===========================================')
            print('在文件【%s】中找到关键字【%s】' % (each_txt_file, key))

            if detail in ['YES','Yes','yes']:
                print_pos(key_dict)



key = input('请将该脚本防于待查找的文件夹内, 请输入关键字: ')
detail = input('请问是否需要打印关键字【%s】在文件中的具体位置(YES/NO):' % key)
search_files(key, detail)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值