I feel that assigning files, and folders and doing the += [item] part is a bit hackish. Any suggestions? I’m using Python 3.2
JavaScript
x
14
14
1
from os import *
2
from os.path import *
3
4
def dir_contents(path):
5
contents = listdir(path)
6
files = []
7
folders = []
8
for i, item in enumerate(contents):
9
if isfile(contents[i]):
10
files += [item]
11
elif isdir(contents[i]):
12
folders += [item]
13
return files, folders
14
Advertisement
Answer
Take a look at the os.walk
function which returns the path along with the directories and files it contains. That should considerably shorten your solution.