Three Ways of Traversing Disk Directory in python

  • 2021-10-24 23:17:03
  • OfStack

Directory depth traversal
Recursion uses the stack to traverse the disk
Width traversal disk
Traversing Disks with Queues

Deep traversal

Recursion


import os

def get_files(path):
  #  Judge whether the path exists or not , If it does not exist, the function ends directly 
  if not os.path.exists(path):
    print(' Path does not exist ')
    return
  #  Determine whether the path is a folder 
  if not os.path.isdir(path):
    print(' The path is 1 Files ')
    return
  #  At this point, the path is 1 Folders 
  #  Gets the name of the file or folder in the folder 
  file_list = os.listdir(path)
  #  Traverse folders 
  for filename in file_list:
    #  Splice paths to get the file path under each secondary directory 
    subpath = os.path.join(path,filename)
    if os.path.isfile(subpath):
      if os.path.splitext(subpath)[1] == '.py':
        print('python Documents: {}'.format(subpath))
    else:
      #  If filename Is a folder, call the function to continue traversing 
      get_files(subpath)

Traverse the disk with the stack

Features of stack: first in, then kitchen, last in, first out
Principle: path is returned to path after being deleted by pop for the first time, traversing the files under the directory. If the folder is added to the list, pop is to delete the last 1-bit element and traverse the last 1-bit folder every time, so every round will traverse the folder under the secondary directory before traversing the next secondary directory


import os

def get_files(path):
  #  Judge whether the path exists or not 
  if not os.path.exists(path):
    print(' Path does not exist ')
    return
  if not os.path.isdir(path):
    print(' The path is 1 Folders ')
    return
  #  Create 1 List as stack 
  stack = [path]
  #  Take out the elements from the stack 
  while len(stack) != 0:
    path = stack.pop()
    file_list = os.listdir(path)
    for filename in file_list:
      subpath = os.path.join(path,filename)
      if os.path.isfile(subpath):
        print('python Documents: {}'.format(subpath))
      else:
        stack.append(subpath)

Width traversal disk

Traversing Disks with Queues


import os
import collections


def get_py_file(path):
  #  Judge whether the path exists or not 
  if not os.path.exists(path):
    print(' Path does not exist ')
    return
    #  Determine whether the path is a folder 
  if os.path.isfile(path):
    print(' The path is a file ')
    return
    # path Yes 1 Folders 

  #  Definition 1 Empty pairs of columns 
  queue = collections.deque()
  queue.append(path)
  while len(queue) != 0:
    #  Gets the first from the queue 1 Elements 
    path = queue.popleft()
    #  Get all the contents in the directory 
    filelist = os.listdir(path)
    #  Traversal 
    for filename in filelist:
      #  Splice 
      filepath = os.path.join(path, filename)
      if os.path.isfile(filepath):
        if os.path.splitext(filepath)[1] == '.py':
          print(filepath)
      else:
        queue.append(filepath)

The above is python traversal disk directory 3 methods of the details, more about python traversal disk directory information please pay attention to other related articles on this site!


Related articles: