the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ. < br / > you can use OS listdir() function to traverse the current directory files, do not need the recursive traversal subdirectory. This function returns a list of the names of all files and subdirectories in the specified directory.
For example, the following code lists all files and subdirectories in the current directory:import os
current_dir = '.' # 当前目录
for filename in os.listdir(current_dir):
path = os.path.join(current_dir, filename)
if os.path.isfile(path):
print('File:', filename)
elif os.path.isdir(path):
print('Directory:', filename)
In the above code, you first use the os.listdir() function to get a list of the names of all the files and subdirectories in the current directory. Then, use the os.path.join() function to concatenate the directory and file names into a full path. Next, the os.path.isfile() and os.path.isdir() functions are used to determine whether the path corresponds to a file or a subdirectory and process accordingly.