31 lines
951 B
Python
31 lines
951 B
Python
import os
|
|
|
|
|
|
def save_file_list(root_dir, output_file, markdown=False):
|
|
lines = []
|
|
|
|
for dirpath, dirnames, filenames in os.walk(root_dir):
|
|
rel_path = os.path.relpath(dirpath, root_dir)
|
|
indent = ' ' * (rel_path.count(os.sep)) if rel_path != '.' else ''
|
|
|
|
if markdown:
|
|
if rel_path != '.':
|
|
lines.append(f"{indent}- {os.path.basename(dirpath)}/")
|
|
for filename in filenames:
|
|
file_path = os.path.join(rel_path, filename)
|
|
if markdown:
|
|
lines.append(f"{indent} - {filename}")
|
|
else:
|
|
lines.append(os.path.normpath(os.path.join(rel_path, filename)))
|
|
|
|
with open(output_file, 'w', encoding='utf-8') as f:
|
|
f.write('\n'.join(lines))
|
|
print(f"📁 文件目录已保存到: {output_file}")
|
|
|
|
|
|
# 示例调用
|
|
if __name__ == "__main__":
|
|
# 输出纯文本
|
|
save_file_list('../docs', '目录.txt', markdown=False)
|
|
|