1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import os

# 设置要读取的资料夹路径
source_folder = r"C:\Users\xxxx\Downloads"
output_file = r"C:\Users\xxxx\Downloads\file_list.txt"

def list_all_files(folder_path):
"""
遍历整个文件夹,包括所有子文件夹,列出所有文件的完整路径
"""
file_names = []

for root, dirs, files in os.walk(folder_path):
# 记录当前目录名称
relative_path = os.path.relpath(root, folder_path)
if relative_path != ".":
file_names.append(f"\n=== 目录: {relative_path} ===\n")

# 记录文件名称
for file in files:
file_path = os.path.join(relative_path, file) if relative_path != "." else file
file_names.append(file_path)

return "\n".join(file_names)

# 获取完整的文件清单并写入 file_list.txt
file_list = list_all_files(source_folder)

with open(output_file, "w", encoding="utf-8") as f:
f.write(file_list)

print(f"完整的文件清单已输出至 {output_file}")