Python3 文件操作的一个例子

本文介绍了一个Python程序,用于在指定目录下递归查找包含特定关键字的文本文件,并精确记录关键字出现的位置信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:用户输入关键字以及开始搜索的路径(递归查找),显示含有该关键字的文本文件(.txt后缀),并显示所在的位置(第几行第几个字符)

显示如下:

请输入待查找的目录:/home/jason/code_python/A
请输入待查找的关键字:jason
{‘3.txt’: {1: [0], 4: [0], 6: [0], 7: [9], 8: [9], 9: [9, 19, 30]}, ‘2.txt’: {1: [0], 4: [0], 6: [0], 7: [9], 8: [9], 9: [9, 19, 30]}, ‘1.txt’: {1: [0], 4: [0], 6: [0], 7: [9], 8: [9], 9: [9, 19, 30]}}
在3.txt文件中找到关键字:jason
关键字出现在第 1 行,第 [0] 个位置
关键字出现在第 4 行,第 [0] 个位置
关键字出现在第 6 行,第 [0] 个位置
关键字出现在第 7 行,第 [9] 个位置
关键字出现在第 8 行,第 [9] 个位置
关键字出现在第 9 行,第 [9, 19, 30] 个位置
在2.txt文件中找到关键字:jason
关键字出现在第 1 行,第 [0] 个位置
关键字出现在第 4 行,第 [0] 个位置
关键字出现在第 6 行,第 [0] 个位置
关键字出现在第 7 行,第 [9] 个位置
关键字出现在第 8 行,第 [9] 个位置
关键字出现在第 9 行,第 [9, 19, 30] 个位置
在1.txt文件中找到关键字:jason
关键字出现在第 1 行,第 [0] 个位置
关键字出现在第 4 行,第 [0] 个位置
关键字出现在第 6 行,第 [0] 个位置
关键字出现在第 7 行,第 [9] 个位置
关键字出现在第 8 行,第 [9] 个位置
关键字出现在第 9 行,第 [9, 19, 30] 个位置

具体实现如下:

#coding=utf-8
'''
用户输入关键字以及开始搜索的路径(递归查找),显示含有该关键字的文本文件(.txt后缀),
并显示所在的位置(第几行第几个字符)
'''
import os
import sys
sys.setrecursionlimit(1000)  # set the maximum depth as 1500

file_path = input('请输入待查找的目录:')
file_name = input('请输入待查找的关键字:')

file_index = {}
def file_find(file_path,file_name):
    if os.path.isdir(file_path):
        file_list = os.listdir(file_path)
        for each in file_list:
            temp_dir = file_path + os.sep + each
            if os.path.isdir(temp_dir):
                ##递归
                temp = file_find(temp_dir,file_name)
            elif os.path.isfile(temp_dir):
                (name,type) = os.path.splitext(each)
                if type == '.txt':
                    #读取txt文档,查找是否有匹配的字符串
                    num_index = 0 #行号
                    f = open(temp_dir,'r')
                    #每个文件中出现关键词的位置以dict存储,
                    #key=行号,value=位置,也就是list_location
                    dict_location = {} 
                    for eachline in f:
                        num_index += 1
                        list_location = []#记录每一行出现关键词的位置,以list存储
                        location = eachline.find(file_name)
                        while location != -1:
                            list_location.append(location)
                            location = eachline.find(file_name,location+1) #从下一个位置开始查找
                        if len(list_location) != 0:
                            dict_location[num_index] = list_location #存储在dict中
                    file_index[each] = dict_location #将查找到的位置放进字典中存储
                    f.close()
    else:
        print('{}不是一个目录'.format(file_path))

'''
执行
例子:
file_path = '/home/jason/code_python/A'
file_name = 'jason'
'''
file_find(file_path,file_name)
print(file_index)
'''
按格式输出
{'2.txt': {1: [0], 4: [0], 6: [0], 7: [9], 8: [9], 9: [9, 19, 30]}
'''
for k,v in file_index.items():
    print('在{file}文件中找到关键字:{file_name}'.format(file = k,file_name = file_name))
    for k1,v1 in v.items():
        print('关键字出现在第 {line} 行,第 {location} 个位置'.format(line= k1,location = v1))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值