python lists all files or directories in the directory (including subdirectories)

#!/usr/bin/python3

# -*- coding: UTF-8 -*-

 

import re

import os

import time

 

def print_files(path):

    lsdir = os.listdir(path)

    dirs = [i for i in lsdir if os.path.isdir(os.path.join(path, i))]    if dirs:

        for i in dirs:

            print_files(os.path.join(path, i))

    files = [i for i in lsdir if os.path.isfile(os.path.join(path,i))]

    #for f in files: # List all files (including files in subdirectories)

    for f in dirs: # List all directories (including subdirectories directory)

        sss = (os.path.join(path, f))

        if os.path.isdir (sss): # judge whether a directory path

            print (sss)  

    return

 

print_files ( '/ test / test1 /') # List all files or directories under '/ test / test1 /' directory (including subdirectories)

Guess you like

Origin blog.csdn.net/qq_35751770/article/details/93734590