鱼C论坛

 找回密码
 立即注册
查看: 1501|回复: 6

零基础35章第四题 (第二次求助)

[复制链接]
发表于 2019-3-20 10:01:07 | 显示全部楼层 |阅读模式
20鱼币
各位大哥,小弟在做第四题时统计代码行数时,因为只学过python,所以只想统计py文件。但是结果每次都是只能统计出第一个文件夹的文件,递归无法实现。请问这是为什么
import os
import easygui as g

def filesearch (filepath):   
    f1 = os.listdir(filepath) ##打开初始文件夹##
    count = 0 ##用于统计行数##
    list1 = [] ##用于列出所有py文件名##
    for each in f1:
        if os.path.splitext(each)[1] == ".py":  ##若该文件是py 文件则打开文件,统计行数。##
           
            list1.append(filepath + "\" +each + " \n")
            f = open (filepath+"\" +each, encoding='UTF-8')
            for eachline in f:
                count = count +1
            f.close()
        if os.path.isdir(filepath + "\"+ each) :##若该文件文件夹,则用递归函数进入文件夹统计行数。##
            
            filesearch (filepath+ "\"+ each)
            os.chdir(os.pardir)
            return list1
            return count
    msg = "Total %s lines" % count + "Progress: " + str(count/1E6) +"\n There are " +\
            str(1E6-count) + " lines left."
    g.textbox(msg, "Final Results", list1) ##显示结果。##

path = g.diropenbox()
filesearch(path)
   

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-3-20 11:15:41 | 显示全部楼层
本帖最后由 风丶少 于 2019-3-20 11:29 编辑

这个是小甲鱼的答案吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-3-20 11:30:24 | 显示全部楼层
本帖最后由 jackz007 于 2019-3-20 11:32 编辑
  1. import os , os . path

  2. def filesearch (filepath):   
  3.     count = 0
  4.     list1 = []
  5.     for each in os . listdir(filepath):
  6.         path = filepath + '\\' + each                            # 必须是 '\\'
  7.         if os . path . isfile(path):                             # 判断 python 源程序的基本前提是:path 必须是一个文件
  8.             if os . path . splitext(each)[1] . lower() == ".py": # 文件名忽略大小写
  9.                 list1 . append(path)
  10.                 f = open(path , encoding = 'UTF-8')
  11.                 for eachline in f:
  12.                     count +=  1
  13.                 f . close()
  14.         elif os . path . isdir(path) :
  15.             vc , vl = filesearch(path)
  16.             count += vc
  17.             list1 += vl
  18.     return count , list1

  19. if __name__ == '__main__':
  20.     count , list1 = filesearch(".")
  21.     n = 0
  22.     for x in list1:
  23.         print  "[%6d]: %s" % (n + 1 , x)
  24.         n += 1
  25.     print 'count = %d\n' % count
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-3-22 14:17:26 | 显示全部楼层
你递归目录的时候没有return 返回值,它就算正常递归你也无法接收呀
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-3-22 21:08:26 | 显示全部楼层
return count, list1     #这是正确的

return list1
return count    #分行写是错误的,已经执行了上一句了,下一名就不执行了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-3-24 11:38:26 | 显示全部楼层
  1. import os
  2. import easygui as g

  3. def filesearch (filepath):   
  4.     f1 = os.listdir(filepath) ##打开初始文件夹##
  5.     count = 0 ##用于统计行数##
  6.     list1 = [] ##用于列出所有py文件名##
  7.     for each in f1:
  8.         if os.path.splitext(each)[1] == ".py":  ##若该文件是py 文件则打开文件,统计行数。##
  9.            
  10.             list1.append(filepath + "\" + each + " \n")
  11.             f = open (filepath+"\" +each, encoding='UTF-8')
  12.             for eachline in f:
  13.                 count = count +1
  14.             f.close()
  15.         if os.path.isdir(filepath + "\"+ each) :##若该文件文件夹,则用递归函数进入文件夹统计行数。##
  16.             
  17.             filesearch (filepath+ "\"+ each)
  18.             os.chdir(os.pardir)
  19.             return list1
  20.             return count
  21.     msg = "Total %s lines" % count + "Progress: " + str(count/1E6) +"\n There are " +\
  22.             str(1E6-count) + " lines left."
  23.     g.textbox(msg, "Final Results", list1) ##显示结果。##

  24. path = g.diropenbox()
  25. filesearch(path)
  26.    
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-3-29 15:10:38 | 显示全部楼层
两个return没看懂 到底要返回什么 应该只要返回count吧

都写在'若是文件夹'的if里面了 对应的是filesearch(filepath)函数的返回值 那么在第一次打开文件夹之后执行filesearch (filepath+ "\"+ each)之后 list1和count就归零了 前边白做=。=?

然后目录下第一个子文件夹打开完了 执行到return (只返回了个list1)其他文件夹就 不管了呗..

建议重新理下逻辑 打开文件夹-遍历文件夹-打开文件-统计代码行数 这样
另外遍历文件夹可以试试os.path.walk(path) 返回是个列表 小甲鱼哪次课后就用过来着
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-14 17:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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