Stack: Meet the characteristics --> First in, last out, similar to the bullet clip in our lives
【note】
For the stack structure: Python does not encapsulate specific functions for it, we can use list (list) to simulate the characteristics of the stack
Use the list object to simulate the characteristics of the stack structure to access data: first in, last out
# Define a list object,stack(Variable name, reference name)
stack=[]
# Add data to the stack(Simulation push)
stack.append('A')print(stack)
stack.append('B')print(stack)
stack.append('C')print(stack)
# Pop data from the stack(Mock popup)
obj=stack.pop()print('pop up:'+obj)
obj=stack.pop()print('pop up:'+obj)
obj=stack.pop()print('pop up:'+obj)
Features of simulated stack structure to achieve deep traversal of directory traversal
import os
# Custom function: to achieve deep traversal of directory levels
def getAllFileST(path):
# Define a list
lt=[]
# Will path(String, absolute path)Push
lt.append(path)
# Determine the number of loop executions according to the length of lt
whilelen(lt)!=0:
# Pop the data in lt onto the stack
file_path=lt.pop()
# Get file_Word content in path(Files, subdirectories)Return as a list
file_list=os.listdir(file_path)
# Loop processing file_list
for file in file_list:
# Restore each element to an absolute path value
fileAbsPath=os.path.join(file_path,file)'''
Is it a file or a directory?
If it is a file, just print the file name
If it is a directory, print the directory first and then push it on the stack
'''
if os.path.isfile(fileAbsPath):print('file:'+file)else:print('table of Contents:'+file)
lt.append(fileAbsPath)
p=r'a.txt'getAllFileST(p)
Recommended Posts