python实现递归加载目录下和子目录下所有指定类型文件

740阅读 0评论2022-08-19 zenith518
分类:Python/Ruby

一个类中的代码片段,记录一下自己的递归实现,以备后查。

点击(此处)折叠或打开

  1. def loadingYamlFile(self, pathstr: str) -> str:
  2.         oldcurpath = self.curpath
  3.         if self.level == 0:
  4.             self.curpath = self.targetpath
  5.         else:
  6.             self.curpath = self.curpath + "\\" + pathstr
  7.         files = os.listdir(self.curpath)
  8.         for f in files:
  9.             if os.path.isdir(self.curpath + "\\" + f):
  10.                 if f[0] == ".":
  11.                     pass
  12.                 else:
  13.                     self.level = self.level + 1
  14.                     self.curpath = self.loadingYamlFile(f)

  15.             if os.path.isfile(self.curpath + "\\" + f) and "yaml" in f:
  16.                 filename = self.curpath + "\\" + f
  17.                 self.yamlfilelist.append(filename)
  18.         return oldcurpath

上一篇:Python计算程序运行消耗时间
下一篇:pip自动生成requirements