鱼C论坛

 找回密码
 立即注册
查看: 1045|回复: 3

第030文件系统最后一题

[复制链接]
发表于 2018-6-29 12:32:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import os
def print_key_dict(key_dict):
    keys = key_dict.keys()
    keys = sorted(keys)
    for i_key in keys:
        print('出现在%行第%s个位置'%(i_key,str(key_dict[i_key])))
def pos_line(line,key):
    pos = []
    begin = line.find(key)
    while begin != -1:
        pos.append(begin+1)
        begin = line.find(key,begin+1)
    return pos

def save_in_file(files_name,key):
    f = open(files_name)
    key_dict = {}
    count = 0
    for each_txt_line in f:
        count += 1
        if key in each_txt_line:
            pos = pos_line(each_txt_line,key)

            key_dict[count] = pos
    f.close()
    return key_dict
def save_file(key,detail):
    txt_file = []
    all_file = os.walk(os.getcwd())
    for i in all_file:
        for each_file in i[2]:
            if os.path.splitext(each_file) == '.txt':
                each_file = os.path.join(i[0],each_file)
                txt_file.append(each_file)

    for each_txt_file in txt_file:
        key_dict = save_in_file(each_txt_file,key)
        if key_dict:
            print('文件%s中有关键字%s'%(each_txt_file,key))
            if detail in ['yes']:
                print_key_dict(key_dict)
key = input('请输入关键字:')
detail = input('请输入是否打印:')

save_file(key,detail)
#这代码有啥问题吗 为啥我的执行后没报错 也没把结果打印出来呢 啥返回结果都没有 求大神解答
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-6-29 13:16:22 | 显示全部楼层
你要保证你执行程序的目录下有txt文件
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-6-29 13:36:04 | 显示全部楼层
凌九霄 发表于 2018-6-29 13:16
你要保证你执行程序的目录下有txt文件

用答案去找可以找到 自己写的没有返回结果
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-29 17:29:58 | 显示全部楼层
在你的那个贴子上有
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-12-31 12:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表