python遍历文件夹找出文件夹后缀为py的文件方法

yipeiwu_com5年前Python基础

大学毕业, 想看看大学写了多少行代码。

#coding=utf-8
import os
class Solution:
 def __init__(self):
  self.dirPath = []
 
 def numberOfCode(self,path):
  for dir in os.listdir(path):
   childDir = os.path.join(path,dir)
   if os.path.isdir(childDir):
    self.numberOfCode(childDir)
   else:
    if childDir[-2:] == "py":
     self.dirPath.append(childDir)
  return self.dirPath
 
 def setCode(self):
  with open("/home/code.py","ab+") as f:
   for file in self.dirPath:
    content = open(file,"r").read()
    f.write(content)
s = Solution()
s.numberOfCode("/home/py/")
s.setCode()

以上这篇python遍历文件夹找出文件夹后缀为py的文件方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python中import reload __import__的区别详解

import 作用:导入/引入一个python标准模块,其中包括.py文件、带有__init__.py文件的目录(自定义模块)。 import module_name[,module...

Python自定义函数的创建、调用和函数的参数详解

函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。函数能提高应用的模块性,和代码的重复利用率。你已经知道Python提供了许多内建函数,比如print()。但你也可以自己...

Python实现图像去噪方式(中值去噪和均值去噪)

Python实现图像去噪方式(中值去噪和均值去噪)

实现对图像进行简单的高斯去噪和椒盐去噪。 代码如下: import numpy as np from PIL import Image import matplotlib.pyplo...

浅析Python中的for 循环

Python for 和其他语言一样,也可以用来循环遍历对象,本文章向大家介绍Python for 循环的使用方法和实例,需要的朋友可与参考一下。 一个循环是一个结构,导致第一个程序要重...

Python for循环生成列表的实例

一般Python for语句前不加语句,但我在机器学习实战中看到了这两条语句: featList = [example[i] for example in dataSet] clas...