SoFunction
Updated on 2024-10-29

python to get all files with a specified suffix under a specified path

This article example describes the method of python to get all the files with the specified suffix under the specified path. Shared for your reference. Specific implementation methods are as follows:

# Get all the files in the specified path with the specified suffix.
# dir Specify the path
# ext Specify a suffix, chained lists & without dots Or don't specify. Example: ['xml', 'java']
def GetFileFromThisRootDir(dir,ext = None):
  allfiles = []
  needExtFilter = (ext != None)
  for root,dirs,files in (dir):
    for filespath in files:
      filepath = (root, filespath)
      extension = (filepath)[1][1:]
      if needExtFilter and extension in ext:
        (filepath)
      elif not needExtFilter:
        (filepath)
  return allfiles

I hope that what I have described in this article will help you in your Python programming.