I need to non-recursively bypass the directory and get a list of folders and files from it. I found an example, but something it does not work for me.

recursive = 0 dst_sav = "D:\\savorsk" src = "D:\\Project\\2014\\archdata" for root, dirs, files in os.walk(src): if not recursive: while len(dirs) > 0: dirs.pop() print dirs 

recursive = 0 I already added this myself because obviously, the variable is set here. Although I could be wrong. However, the script I have displays only one directory and that's it. What's wrong?

I need to get a list of files and folders in it. Non-recursive os.walk ()

  • one
    os.listdir? - dzhioev

1 answer 1

 for path, dirs, files in os.walk(.....) 

path - current directory where the loop is looking (string)
dirs - an array of directory names in the current directory
files - an array of file names in the current directory

 for path, dirs, files in os.walk(...): for fname in files: print os.path.join(path, fname)