鱼C论坛

 找回密码
 立即注册
查看: 683|回复: 2

[已解决]查找文件中的关键字

[复制链接]
发表于 2018-7-20 20:22:13 | 显示全部楼层 |阅读模式

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

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

x
import os
def search_files(key,detail):
    all_txt_files=[]
    for each_file in os.listdir(os.curdir):
        if os.path.splitext(each_file)[1]=='.txt':
            all_txt_files.append(each_file)
            for each_txt_file in all_txt_files:
                search_in_file(each_txt_file,key)#调取一个可以找出文件中关键字的函数
                if detail in ['yes','Yes','YES']:
                    print('该文件所在位置%s有关键字'%(each_txt_file,key))
                    for each in line_dict.keys():
                        ptint('在该文件中第%s行%s位置'%(each,line_dict[each]))#如果detail是yes  调用一个打印函数
        if os.path.isdir(each_file):
            search_files(key,detail)
            os.chdir(os.pardir)
            
            
def search_in_file(each_file,key):
    f1=open(each_file)
    f=f1.read()
    line_dict=dict()
    count=0
    for each_line in f:
        count+=1
        
        if key in each_line:
            line_dict[count]=each_line.count(key)

    f1.close()
    return line_dict

        
key=input('请将该代码放在待查找的文件夹中,请输入关键字:')
detail=input('请问是否需要打印关键字%s在文件中的具体位置(yes/no)'%key)
search_files(key,detail)





请将该代码放在待查找的文件夹中,请输入关键字:愿
请问是否需要打印关键字愿在文件中的具体位置(yes/no)yes
Traceback (most recent call last):
  File "C:/Users/lenovo/Desktop/查找关键字.py", line 35, in <module>
    search_files(key,detail)
  File "C:/Users/lenovo/Desktop/查找关键字.py", line 10, in search_files
    print('该文件所在位置%s有关键字'%(each_txt_file,key))
TypeError: not all arguments converted during string formatting
>>>
最佳答案
2018-7-20 21:31:22
你这代码问题太多。
1就像楼上说的
print('该文件所在位置%s有关键字'%(each_txt_file,key)) 这本该只有1个参数

第2 递归遍历目录查找文件
def search_files(key,path=''):
    if path!='':
        os.chdir(path)

    for each_file in os.listdir(os.curdir):
        if os.path.splitext(each_file)[1]=='.txt':
            all_txt_files.append(each_file)
            for each_txt_file in all_txt_files:
                search_in_file(each_txt_file,key)#调取一个可以找出文件中关键字的函数
                if detail in ['yes','Yes','YES']:
                    print('该文件所在位置%s有关键字'%(each_txt_file,key))
                    for each in line_dict.keys():
                        ptint('在该文件中第%s行%s位置'%(each,line_dict[each]))#如果detail是yes  调用一个打印函数
        if os.path.isdir(each_file):
            search_files(key,each_file)

            os.chdir(os.pardir)
            
第3 文件编码问题

提供给你参考,具体自己捉摸。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-7-20 21:25:03 | 显示全部楼层
print('该文件所在位置%s有关键字'%(each_txt_file,key)) 这句前面少写了个格式化符号吧,它后面有两个参数
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-20 21:31:22 | 显示全部楼层    本楼为最佳答案   
你这代码问题太多。
1就像楼上说的
print('该文件所在位置%s有关键字'%(each_txt_file,key)) 这本该只有1个参数

第2 递归遍历目录查找文件
def search_files(key,path=''):
    if path!='':
        os.chdir(path)

    for each_file in os.listdir(os.curdir):
        if os.path.splitext(each_file)[1]=='.txt':
            all_txt_files.append(each_file)
            for each_txt_file in all_txt_files:
                search_in_file(each_txt_file,key)#调取一个可以找出文件中关键字的函数
                if detail in ['yes','Yes','YES']:
                    print('该文件所在位置%s有关键字'%(each_txt_file,key))
                    for each in line_dict.keys():
                        ptint('在该文件中第%s行%s位置'%(each,line_dict[each]))#如果detail是yes  调用一个打印函数
        if os.path.isdir(each_file):
            search_files(key,each_file)

            os.chdir(os.pardir)
            
第3 文件编码问题

提供给你参考,具体自己捉摸。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-16 02:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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