Python 打印给定目录的文件树
2021/7/6 20:44:12
本文主要是介绍Python 打印给定目录的文件树,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
版权声明:本文为CSDN博主「MortonWang」的原创文章, 原文链接:https://blog.csdn.net/qq_34740599/article/details/106864442
利用 os 库自带的函数,递归实现目录及文件的遍历,为了输出文件树,输出的时候配合 deep 增加了一些样式控制。
代码的7–15行是为了调整遍历的顺序,优先遍历文件夹
import os def traverse_dir(current_dir, deep=0): """ current_dir: 绝对路径: "./" 或当前路径: 'D:/picture' """ dir_list = os.listdir(current_dir) # traverse folder first. path_list, file_list = [], [] for dir in dir_list: path = os.path.join(current_dir, dir) if os.path.isdir(path): path_list.append(dir) else: file_list.append(dir) dir_list = path_list + file_list # traverse all dir. for dir in dir_list: path = os.path.join(current_dir, dir) if os.path.isdir(path): # do something to this directory print("\t" * deep, dir) traverse_dir(path, deep + 1) if os.path.isfile(path): # do something to this file print("\t" * deep, "|--", dir)
执行 traverse_dir("./")
后,遍历效果如下:
.idea inspectionProfiles |-- profiles_settings.xml |-- geo_IF.iml |-- markdown-navigator-enh.xml |-- markdown-navigator.xml |-- misc.xml |-- modules.xml |-- workspace.xml dataset_cmu corpus |-- content_all.txt |-- content_all_process.txt |-- model_dim_512_epoch_40.bin |-- model_dim_512_epoch_40.bin.trainables.syn1neg.npy |-- model_dim_512_epoch_40.bin.wv.vectors.npy edge |-- edge_pair.ungraph |-- out_of_order.emd |-- dump_doc_dim_512.pkl |-- node2vec_dim128.npy |-- user_info.dev.gz |-- user_info.test.gz |-- user_info.train.gz data_process __pycache__ |-- dataloader.cpython-37.pyc |-- doc2vec.cpython-37.pyc |-- kdtree.cpython-37.pyc |-- node2vec.cpython-37.pyc |-- dataloader.py |-- doc2vec.py |-- handle_n2v.py |-- kdtree.py |-- node2vec.py |-- preprocess.py pic |-- inf_dist_log.png |-- n2v_cluster_3D.png |-- n2v_hop3.png |-- pos_overall_neg.png |-- sgc_cluster_3D.png |-- sgc_hop3.png plot_data |-- cluster_coordinate.dump |-- degree_neighbor.dump |-- distance_array.txt |-- n2v_all_inf.txt |-- n2v_cluster_inf.dump |-- n2v_grade_inf_gap1.txt |-- n2v_hop3_inf.dump |-- sgc_all_inf.txt |-- sgc_cluster_inf.dump |-- sgc_grade_inf_gap1.txt |-- sgc_hop3_inf.dump plot_functions |-- plot_cluster.py |-- plot_comparison.py |-- plot_degree.py |-- plot_geo_dist.py Res_inf_N2V |-- inf_of_a_test_point0.txt Res_inf_SGC |-- inf_of_a_test_point0.txt |-- inf_of_a_test_point1.txt __pycache__ |-- hessians.cpython-37.pyc |-- my_utils.cpython-37.pyc |-- get_all_file_in_path.py |-- get_influ_matrix.py |-- hessians.py |-- main_N2V_inf.py |-- main_SGC_inf.py |-- my_utils.py |-- readme.md |-- requirements.txt
这篇关于Python 打印给定目录的文件树的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python