sam_wu 发表于 2020-3-28 16:40:17

python第30讲课后作业,查找字符串并输出行号和列号,为什么我写的代码不能print

本帖最后由 qiuyouzhi 于 2020-3-28 17:02 编辑

问题:请问能不能帮我看一下 row_column()函数没有输出任何文件
思路:1.先建立一个函数查找目录下所有的txt文件(包括文件夹里面的)文件名存储在temp列表里
2. 第二个函数,row_column()先遍历每个txt文件,看看目标字符串是不是在这个文件里面,如果在里面遍历每一行,用count统计行号,用index统计列号
3.运行后,第二个函数完全没有执行,不知道哪里出了问题
谢谢
运行结果:


代码
import os

def search_file():
    os.curdir
    a = os.getcwd()
   
    for each_file in os.listdir(os.curdir):
      
      if os.path.splitext(each_file) == '.txt':#查找出目录下所有的.txt文件
            temp.append(each_file)
         
      if os.path.isdir(each_file):
            search_file(each_file)
            os.chdir(os.pardir)
            
def row_column():             #确定行号和列号
   
    for each_file1 in temp:   #调用在列表temp下保持的每个txt文件
      
      f = open(str(each_file1))
      lines = f.readlines()
      for each_row in lines:   #每个txt文件的每一行
         
            if target not in each_row:
                count += 1
            else:
                pos = each_row.index(target) #把列号保存在列表pos里
               
                print('========')
                print('在文件'+ each_file1 + '中找到关键字' + target)
                for count1 in pos.keys():#输出每一个包含target字符串的行和列
                  print('关键字出现在第:' + count + '行,第' + post+ '个位置')
                count += 1
      f.close()
count = 0
pos = {}                  
temp = []   
lines = []
a =()
target = input('请输入要查找的字符串:')
check = input('是否打印关键字具体位置yes/no:')
if check == 'yes':
    search_file()
    row_column()

BngThea 发表于 2020-3-28 16:54:55

先吐槽一句:好多全局变量,为什么不做参数传递到函数

你先检查一下temp变量在search_file函数后存的元素是不是每一个txt文件
然后检查打开的文件中的内容

sam_wu 发表于 2020-3-28 17:10:50

BngThea 发表于 2020-3-28 16:54
先吐槽一句:好多全局变量,为什么不做参数传递到函数

你先检查一下temp变量在search_file函数后存的元 ...

{:5_105:}第一次没有什么经验,我先去改一下,参数好像是有问题。。

sam_wu 发表于 2020-4-2 18:02:20

这个是我修改后的代码,可以实现的,但是有些地方比较乱,供参考


import os

def search_file(start_dir):
    os.chdir(start_dir)
   
   
    for each_file in os.listdir(os.curdir):
      
      if os.path.splitext(each_file) == '.txt':#查找出目录下所有的.txt文件
            each_file = os.path.join(os.getcwd(),each_file)
            temp.append(each_file)
         
      if os.path.isdir(each_file):
            search_file(each_file)
            os.chdir(os.pardir)


def pos_in_line(each_row,target):   #查找每一行
    column = []
    begin = each_row.find(target)
    while begin != -1:
      column.append(begin + 1)
      begin = each_row.find(target, begin + 1)
      

    return column


            
def row_column(target):             #确定行号和列号
   
    for each_file_1 in temp:   #调用在列表temp下保持的每个txt文件
      
      f = open(each_file_1)
      count = 0
      pos = {}
      txt = []
      
      lines = f.readlines()
      for each_row in lines:   #每个txt文件的每一行
            count += 1
            if target in each_row:
                pos = pos_in_line(each_row,target) #把列号保存在字典pos里
                txt.append(each_file_1)
      news_txt = []
      for direction in txt:      #列表去重
            if direction not in news_txt:
                news_txt.append(direction)      

      for i in news_txt:#打印
            print('========')
            print('在文件'+ i + '中找到关键字' + target)
            for count1 in pos.keys():#输出每一个包含target字符串的行和列
            
                print('关键字出现在第:%s行,第%s个位置' %(count1,str(pos)))
            
      f.close()
      
      


   

                  
temp = []   
lines = []
target = input('请输入要查找的字符串:')
check = input('是否打印关键字具体位置yes/no:')
if check == 'yes':
    search_file(os.getcwd())
    row_column(target)

sam_wu 发表于 2020-4-2 18:10:20

BngThea 发表于 2020-3-28 16:54
先吐槽一句:好多全局变量,为什么不做参数传递到函数

你先检查一下temp变量在search_file函数后存的元 ...

temp 变量我忽略了路径。。。所以最开始temp只保存了文件名,没有路径名。
全局变量我已经改写成局部变量了,不过整体代码还是冗余,不够优美。。{:5_105:}
页: [1]
查看完整版本: python第30讲课后作业,查找字符串并输出行号和列号,为什么我写的代码不能print