|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #------显示文件及关键字位置------#
- import os
- def search_file(start_dir):
- os.chdir(start_dir)
- all_files = os.listdir(os.curdir)
- for each_file in all_files:
- if os.path.isfile(each_file):
- ext = os.path.splitext(each_file)[1]
- if ext == '.txt':
- path = os.getcwd() + os.sep + each_file
- all_txt.append(path) # 查找到的所有txt文件路径
- if os.path.isdir(each_file):
- search_file(each_file)
- os.chdir(os.pardir)
- #return all_txt
- key = input('请将该代码放于待查找的文件夹内,请输入关键字:') #输入关键字
- decide = input('请问是否需要打印关键字【%s】在文件中的具体位置(YES/NO):'%key) #是否打印关键字
- all_txt = []
- key_positon = []
- present_dir = os.getcwd() #当前工作目录
- search_file(present_dir) #调用search_file函数
- count = 1
- if decide == 'YES' or decide == 'yes': #如果为真
- for each in all_txt: #遍历所有文件路径列表
- f = open(each)
- length = len(f.readlines())
- f.seek(0)
- for each_line in f:
- if key in each_line:
- position= each_line.find(key) + 1
- key_position.append(position)
- each_line = each_line[position:-1]
- if key in each_line:
- position = each_line.find(key) + 2
- key_position.append(position)
-
-
- print('在文件【%s】中找到关键字【%s】'%(each, key))
- print('关键字出现在第%d行,第%s个位置'%(count,key_position))
-
- count += 1
- key_position = []
- if count == length+1:
- count = 1
- print('==================================================')
-
- f.close()
-
-
-
复制代码
在print('关键字出现在第%d行,第%s个位置'%(count,key_position))这里,key_position是一个列表,但是可以正常运行出结果。
但是在小甲鱼的答案中,
- 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: #如果不等于-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) #打开文件file_name
- count = 0 # 记录行数
- key_dict = dict() # 字典,用户存放key所在具体行数对应具体位置
-
- for each_line in f: #遍历每一行
- count += 1 #count + 1
- if key in each_line: #如果关键字在每一行里面
- pos = pos_in_line(each_line, key) # key在每行对应的位置 调用pos_in_line
- 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]: #i[2]为保存文件的位置
- if os.path.splitext(each_file)[1] == '.txt': # 根据后缀判断是否文本文件
- each_file = os.path.join(i[0], each_file)
- txt_files.append(each_file) #将txt格式保存在txt_files中
- 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) #调用函数search_files
复制代码
print('关键字出现在第 %s 行,第 %s 个位置。' % (each_key, str(key_dict[each_key]))) 这里将key_dict[each_key]转化成了字符串,我试了下,去掉str的话运行就没有输出结果。
求问这是为啥?求各位大佬解答一下
是这样的 print('%s'%content)表明%s这个地方将要传入一个str类型的数据,
如果后面的content不是str类型那么就尝试转化为str,无法转化时会报错
不存在空这种情况的
|
|