|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import os
def print_pos(file_path,lines_pos,lines_key_pos,key):
print('在文件%s中找到关键字【%s】' % (file_path,key))
if decide == 'yes':
print('关键字【%s】在%s行第%s位出现'% (key,lines_pos,lines_key_pos))
def char_pos(key,each_lines):
print('1')
start = 0
pos = []
for i in each_lines:
start += 1
if key == i:
pos.append(start)
return pos
def read_each_lines(file_name,key):
print('2')
lines = 0
lines_pos_list = []
file = open(file_name)
for each_lines in file:
lines += 1
if key in each_lines:
lines_pos_list.append(lines)
pos = char_pos(key,each_lines)
print_pos(file_name,lines_pos_list,pos,key)
return current_path(key)
#获取当前路径中的所有文件夹以及文件
def current_path(key):
txt_path_list= [] #保存txt文件路径
print('3')
for each_path in os.walk('D:\\测试文件'):
for each_file in each_path[2]:
if each_file == '.txt':
temp = os.path.join(each_path[0],each_file)
txt_path_list.append(temp)
key_pos = read_each_lines(temp,key)
key = input('输入关键字')
decide = input('是否显示打印关键的具体位置')
current_path(key)
以上可能还存在其他诸多问题 |
|