python得到文件夹下所有的某类型的文件



def gci(filepath,mylist):
#遍历filepath下所有文件,包括子目录,注意是文件不包含文件夹
  files = os.listdir(filepath)
  for fi in files:
    fi_d = os.path.join(filepath,fi)
    #print("----",fi_d)
    if os.path.isdir(fi_d):
      gci(fi_d,mylist)
    else:
      mylist.append(os.path.join(filepath,fi_d))

def get_all_files(path):
  mylist=[]
  gci(path,mylist)
  print(mylist)
  print(len(mylist))
  txtlist=[i for i in mylist if os.path.splitext(i)[1]=='.txt']     #获得所有的txt
  jsonlist = [i for i in mylist if os.path.splitext(i)[1] == '.json'] #获得所有的json
  print(jsonlist)
  print(txtlist)

import os
def walkmethod(path):
  for fpathe,dirs,fs in os.walk(path):
    for f in fs:
      print (os.path.join(fpathe,f))

get_all_files(r"C:\path\data1")
print("----------------------------")

可以参考下面这个老哥的文章
https://www.cnblogs.com/dreamer-fish/p/3820625.html

你可能感兴趣的:(python编程)