Python "Building a directory tree with simple Os.walk"

Source: Internet
Author: User

The idea is relatively simple, using Os.walk to generate directory information, and then the string operation, so there is no other complex logic, but the resulting effect is relatively simple, see for yourself can ha.

The code is as follows

#-*-Coding:utf-8-*-"Created on July, 2017@author:hehe1234567" Import osdef simple_dir_tree (Ddir): For    dir Path,dirnames,filenames in Os.walk (Ddir.strip (OS.SEP)):        if Dirpath = = Ddir:            string = "for            f in filenames:<" c4/>string = string + ' \ n ' + ' *len (dirpath) + ' |__ ' + str (f)            print Ddir +string        else:            Dirn = Os.path.basenam E (dirpath)            string = "*len (os.path.dirname (dirpath)) + ' |__ ' + Dirn for            f in filenames:                string = string + ' \ N ' + ' *len (dirpath) + ' |__ ' + str (f)            print stringif __name__ = = ' __main__ ':    simple_dir_tree (' C:\\users\\auser . Mey\\desktop\\loadbalance-playbook\\loadbalance-playbook ')

  

The resulting directory tree is as follows:

Python "Building a directory tree with simple Os.walk"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.