Home > Web Front-end > JS Tutorial > body text

Python implements tree printing directory structure_python

不言
Release: 2018-04-02 16:31:20
Original
2119 people have browsed it

This article mainly introduces the relevant code of python tree printing directory structure in detail. It has certain reference value. Interested friends can refer to it.

The example of this article shares python with everyone. The specific code for tree printing directory structure is for your reference. The specific content is as follows

Preface

When I was sorting out the data files in the past two days, I found that there were many clicks one after another. Folder viewing is very tedious, so I wanted to write a tool to recursively print out the tree structure of the file directory. I found some information on the Internet and almost all used os.walk, and debugged it. Later, I found that what was returned seemed to be a "generator", which only required a for loop. However, the structure obtained in this way seemed to be the BFS structure, not the tree structure I wanted. Finally, I finally found out os.listdirThis function, but use it to write a depth-first search, as long as the recursive call can solve my problem.

Code

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#a test for traverse directory

__author__ = 'AlbertS'

import os
import os.path

def dfs_showdir(path, depth):
 if depth == 0:
 print("root:[" + path + "]")

 for item in os.listdir(path):
 if '.git' not in item:
  print("| " * depth + "+--" + item)

  newitem = path +'/'+ item
  if os.path.isdir(newitem):
  dfs_showdir(newitem, depth +1)

if __name__ == '__main__':
 dfs_showdir('.', 0)
Copy after login

Running effect

root:[.]
+--1111.segmentfault.com
| +--01decode.py
| +--01string.txt
| +--1111.segmentfault.com.tar.gz
+--urllib_test.py
+--use_module.py
+--water_deal
| +--water_pouring2.py
+--web
| +--module_test.py
| +--__init__.py
| +--__pycache__
| | +--module_test.cpython-34.pyc
| | +--__init__.cpython-34.pyc
+--web_crawler
| +--bg_teaser.svg
| +--crawler_images
| | +--10393478-1.jpg
| | +--13802226-1.jpg
| | +--169b1b76356f636.jpg
| | +--1a774de56fb4bf2.jpg
| | +--small_event_dft.jpg
| | +--ypy_qr.jpg
| +--crawler_image_test.py
| +--crawler_test.py
| +--crawler_website
| | +--crawler_article_set
| | | +--aiohttp.html
| | | +--asyncio.html
| | | +--async_await.html
| | | +--base64.html
Copy after login

Summary

  • When I first wrote it, I found that I could only recurse one level of folders. Later I found that the problem occurred in the os.path.isdir function.

  • The parameter passed to the os.path.isdir function can only be an absolute path, or a relative path relative to the working directory.

  • With the problems discovered above, the process of splicing newitem variables was born.

Related recommendations:

Python implements the function of merging two files

Python implements the generation function The method of implementing an iterable object

Python implements type checking of instance attributes

The above is the detailed content of Python implements tree printing directory structure_python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!