import os
for folderName, subfolders, filenames in os.walk(r'D:\code\vs_code'):
print('当前文件夹:' + folderName)
for subfolder in subfolders:
print('子目录:' + folderName + ': ' + subfolder)
for filename in filenames:
print('包含文件:' + folderName + ': ' + filename)
print('')
输出:
当前文件夹:D:\code\vs_code
子目录:D:\code\vs_code: client
子目录:D:\code\vs_code: demo
子目录:D:\code\vs_code: map
子目录:D:\code\vs_code: pytest
子目录:D:\code\vs_code: server
子目录:D:\code\vs_code: test
子目录:D:\code\vs_code: thread
当前文件夹:D:\code\vs_code\client
子目录:D:\code\vs_code\client: .vscode
包含文件:D:\code\vs_code\client: client.py
当前文件夹:D:\code\vs_code\client\.vscode
包含文件:D:\code\vs_code\client\.vscode: launch.json
包含文件:D:\code\vs_code\client\.vscode: settings.json
当前文件夹:D:\code\vs_code\demo
包含文件:D:\code\vs_code\demo: demo.py
包含文件:D:\code\vs_code\demo: weather_mini.json
当前文件夹:D:\code\vs_code\map
子目录:D:\code\vs_code\map: .vscode
包含文件:D:\code\vs_code\map: map.py
当前文件夹:D:\code\vs_code\map\.vscode
包含文件:D:\code\vs_code\map\.vscode: launch.json
包含文件:D:\code\vs_code\map\.vscode: settings.json
当前文件夹:D:\code\vs_code\pytest
包含文件:D:\code\vs_code\pytest: pytest.py
当前文件夹:D:\code\vs_code\server
子目录:D:\code\vs_code\server: .vscode
包含文件:D:\code\vs_code\server: server.py
当前文件夹:D:\code\vs_code\server\.vscode
包含文件:D:\code\vs_code\server\.vscode: launch.json
包含文件:D:\code\vs_code\server\.vscode: settings.json
当前文件夹:D:\code\vs_code\test
子目录:D:\code\vs_code\test: .vscode
子目录:D:\code\vs_code\test: __pycache__
包含文件:D:\code\vs_code\test: fuc.py
包含文件:D:\code\vs_code\test: mytest.py
当前文件夹:D:\code\vs_code\test\.vscode
包含文件:D:\code\vs_code\test\.vscode: launch.json
包含文件:D:\code\vs_code\test\.vscode: settings.json
当前文件夹:D:\code\vs_code\test\__pycache__
包含文件:D:\code\vs_code\test\__pycache__: fuc.cpython-37.pyc
当前文件夹:D:\code\vs_code\thread
子目录:D:\code\vs_code\thread: .vscode
子目录:D:\code\vs_code\thread: __pycache__
包含文件:D:\code\vs_code\thread: numbers.json
包含文件:D:\code\vs_code\thread: test.json
包含文件:D:\code\vs_code\thread: thread.py
当前文件夹:D:\code\vs_code\thread\.vscode
包含文件:D:\code\vs_code\thread\.vscode: launch.json
包含文件:D:\code\vs_code\thread\.vscode: settings.json
当前文件夹:D:\code\vs_code\thread\__pycache__
包含文件:D:\code\vs_code\thread\__pycache__: thread.cpython-37.pyc