Python View all directories and files under directory

Source: Internet
Author: User

Python to view all subdirectories and sub-files under the directory

Python Recursive convenience directory structure

Method 1
import JSON, Osdef list_dir (Path, res): forIinchOs.listdir (path): Temp_dir=os.path.join (path, i)ifOs.path.isdir (temp_dir): Temp= {"dirname": Temp_dir,'Child_dirs': [],'Files': []} res['Child_dirs'].append (List_dir (Temp_dir, temp))Else: res['Files'].append (i)returnresdef get_config_dirs (): Res= {'dirname':'Root','Child_dirs': [],'Files': []}  returnList_dir (R'/opt/code/my_code/zk_css', Res)if__name__ = ='__main__': Print (Json.dumps (Get_config_dirs ()))

Reference website: http://lvmy.iteye.com/blog/1258824

Method 2:
Import osdef GCI (filepath): #遍历filepath下所有文件, including subdirectories   = Os.listdir (filepath)  for in  files:    = os.path.join (FILEPATH,FI)     if Os.path.isdir (fi_d):      GCI (fi_d)    Else:      print (Os.path.join (filepath,fi_ d)) #递归遍历/root directory of all files GCI ('/opt/code/my_code/zk_css/common')

Python View all directories and files under directory

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.