[Tutor] folder and file list

Tommy Bell tommy at enkelthed.dk
Tue May 10 14:17:40 CEST 2011


Hey, quite new at python and I have been trying to make a script that 
will list folders and files, but so far its not working
import os, glob

def scandir(path):
     direc = []
     file = []
     for current in glob.glob(os.path.join(path,'*')):
         if os.path.isdir(current):
             direc.append(current) #append to direc list
             scandir(current)
         if os.path.isfile(current): #should test if current is a file, 
but it doesnt, because current is a path
             file.append(current) #append to file list

     for i in file:
         print i
     for j in direc:
         print j

scandir('c:\tmp')

this doesnt work, I know why - but i dont know how to fix it.

The reason it doesnt work is because isfile requires a file, but current 
contains a path.
The objective is to get it to append files to the file list, later i 
wish to figure out how to make it print like so
folder:
     file1
     file2
folder2
     folder2.1
         file1
         file2
etc.

Any ideas? I dont mind reading if i could justbe pointed in the right 
direction

Regards
Tommy


More information about the Tutor mailing list