|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
编写一个程序,用户输入关键字,查找当前文件夹内所有含有该关键字的文本文件(.txt后缀),按要求
显示该文件所在的位置以及关键字在文件中的具体位置(第几行第几个字符)。
求注释!!!求注释!!!求注释!!!
- 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)
- 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)
复制代码
写这玩意真不容 哎~~~ 第一次写注释 希望路过的大神帮忙指正 谢谢
- import os #导入 模块 因为下面要用到 os / os.path
- def print_pos(key_dict): #初始化一个函数print_pos 参数为()key_dict
- keys = key_dict.keys() #返回形参 key_dict 中所有的键
- keys = sorted(keys) # 由于字典是无序的,我们这里对行数进行排序
- for each_key in keys: #迭代读取 每一个键 并赋值给 each_key
- print('关键字出现在第 %s 行,第 %s 个位置。' % (each_key, str(key_dict[each_key]))) #这里打印键(keys)传给第一个%s, 把str(key_dict[each_key])的值传递给第二个%s
- def pos_in_line(line, key): # 这里也是定义一个函数外加两个形参
- pos = [] # 创建一个列表 名字是 pos
- begin = line.find(key) # 这里是find 用法,查找key 是否在line中 ,如果key在line里面返回key位置的索引值,反之返回 -1 ,如果忘记了去列表哪章看看,
- while begin != -1: #这里如果上面 find 找到了key 执行下面的循环
- pos.append(begin + 1) # 用户的角度是从1开始数(索引值都是从0开始的 所以这里要+1,然后把找到key的索引值传递到 pos 列表中)
- begin = line.find(key, begin+1) # 从下一个位置继续查找 (这里还是 find的用法 find有三个可选参数,这里用到了两个。这里继续查找key 看这个字符串里面有没有了,从刚才的位置开始查 ,也就是begin+1的位置)
- return pos # 这里 返回 pos的值了
- def search_in_file(file_name, key): #这里一样定义一个函数 然后有两个 形参
- f = open(file_name) #打开文件 file_name 并赋值给 f
- count = 0 # 记录行数 (这里记录打开文件的行数,之后的循环一次 count 的值就 +1)
- key_dict = dict() # 字典,用户存放key所在具体行数对应具体位置
-
- for each_line in f: # 在上面打开的文件 f 中获取每一行
- count += 1 # 这里读取一行 count 的值 就 +1
- if key in each_line: # 这里如果 键 key 在这(each_line)行里
- pos = pos_in_line(each_line, key) # key在每行对应的位置 (调用上面的函数,把形参换了哦)
- key_dict[count] = pos # ~~~~~~~
- f.close() # 关闭文件
- return key_dict # 返回 key_dict 的值
- def search_files(key, detail): #这里一样定义一个函数 然后有两个 形参
- all_files = os.walk(os.getcwd()) #os.walk() 方法用于通过在目录树中游走输出在目录中的文件名,向上或者向下。 这里加入了 getcwd 应该是返回返回到当前工作目录 然后把目录赋值给all_files
- txt_files = [] #创建列表
- for i in all_files: # 如果i在 all_files 的目录里执行以下内容
- for each_file in i[2]: # 迭代读取i中的内容
- if os.path.splitext(each_file)[1] == '.txt': # 根据后缀判断是否文本文件
- each_file = os.path.join(i[0], each_file) # 合体了 把join括号中的参数 组成路径
- txt_files.append(each_file) # 把each_file 中的内容传进列表
- for each_txt_file in txt_files: #迭代读取列表 txt_files 中的内容
- key_dict = search_in_file(each_txt_file, key) # 调用 search_in_file 函数(注意形参变了哦)
- 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)
复制代码
|
|