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中的__SLOTS__属性使用示例

看python社区大妈组织的内容里边有一篇讲python内存优化的,用到了__slots__。然后查了一下,总结一下。感觉非常有用 python类在进行实例化的时候,会有一个__dict...

Python实现大数据收集至excel的思路详解

一、在工程目录中新建一个excel文件 二、使用python脚本程序将目标excel文件中的列头写入,本文省略该部分的code展示,可自行网上查询 三、以下code内容为:实现从接口获取...

python处理自动化任务之同时批量修改word里面的内容的方法

#同时修改好几个word文档,转换特定的内容 import re import docx doc1=docx.Document('example.docx') spam=['后勤',...

python读取TXT每行,并存到LIST中的方法

python读取TXT每行,并存到LIST中的方法

文本如图: Python: import sys result=[] with open('accounts.txt','r') as f: for line in f: re...

python 定义n个变量方法 (变量声明自动化)

code: for i in range(100): cmd = "t%s = 1" % i exec cmd eval("t%s" % i) print t10 输出...