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

yipeiwu_com6年前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操作Excel之xlsx文件

前言 之前处理excel的读写时用的是xlrd/xlwt,但是这两个库有个缺点就是只对xls的格式处理的比较好,对以xlsx结尾的格式就不行了。由于现在大家使用的都是最新版本的offic...

利用OpenCV和Python实现查找图片差异

利用OpenCV和Python实现查找图片差异

使用OpenCV和Python查找图片差异 flyfish 方法1 均方误差的算法(Mean Squared Error , MSE) 下面的一些表达与《TensorFlow - 协方...

用Python写冒泡排序代码

python代码实现冒泡排序代码其实很简单,具体代码如下所示: 代码Code highlighting produced by Actipro CodeHighlighter (fr...

python自动化报告的输出用例详解

python自动化报告的输出用例详解

1、设计简单的用例 2、设计用例    以TestBaiduLinks.py命名 # coding:utf-8 from selenium import webdriver imp...

python批量读取txt文件为DataFrame的方法

python批量读取txt文件为DataFrame的方法

我们有时候会批量处理同一个文件夹下的文件,并且希望读取到一个文件里面便于我们计算操作。比方我有下图一系列的txt文件,我该如何把它们写入一个txt文件中并且读取为DataFrame格式呢...