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