| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
 本帖最后由 是BB啊 于 2021-4-24 14:31 编辑  
 
不知道我的代码有什么问题,运行后不报错但是不打印,有没有大佬帮忙看看 
 
import os 
def key_count(): 
    time = each_line.count(key) 
    return time 
 
def find_something(start_dir,key): 
    all_file = os.walk(start_dir) 
    for i in all_file: 
        for each_file in i[2]: 
            txt = os.path.splitext(each_file)[1] 
            if txt == '.txt': 
                f = open(each_file) 
                 
                line = 0 
                 
                for each_line in each_file: 
                     
                    line += 1 
                     
                    if key in each_line: 
                        time = key_count() 
                         
                        loc = [] 
                        location = -1 
                         
                        print(format('','=^50')) 
                        print('在文件【%s】中找到关键字【%s】' % (os.getcwd + os.sep + each_file,key)) 
                        for j in range(time): 
                            location = each_line.find(key,location+1) 
                            loc.append(location) 
                        print('关键字出现在第 %s 行,第%s个位置' % (line,location)) 
                         
                f.close() 
 
start_dir = 'D:\\python\\试着写\\saysomething1.txt' 
key = input('请将脚本放于待查找的文件夹内,请输入关键词:') 
find_something(start_dir,key) 
 
 |   
 
 
 
 |