qin_yin 发表于 2020-10-1 13:29:21

为什么我的函数不能调用

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:
            if each_file == '.txt':
                temp = os.path.join(each_path,each_file)
                txt_path_list.append(temp)
                key_pos = read_each_lines(temp,key)


key= input('输入关键字')
decide = input('是否显示打印关键的具体位置')

current_path(key)
以上可能还存在其他诸多问题{:10_277:}

kylin121380 发表于 2020-10-1 13:57:27

if os.path.splitext(each_file) == '.txt':
return current_path(key)???
def char_pos(key,each_lines):也有问题,关键字只有一个字?

qin_yin 发表于 2020-10-1 14:11:51

kylin121380 发表于 2020-10-1 13:57
return current_path(key)???
def char_pos(key,each_lines):也有问题,关键字只有一个字?

{:10_282:}
页: [1]
查看完整版本: 为什么我的函数不能调用