I'm trying to write a program that takes a command line argument, scans through the directory tree provided by the argument and creating a list of every file in the directory, and then sorting by length of files.
I'm not much of a script-guy - but this is what I've got and it's not working:
import sys
import os
from os.path import getsize
file_list = []
#Get dirpath
dirpath = os.path.abspath(sys.argv[0])
if os.path.isdir(dirpath):
#Get all entries in the directory
for root, dirs, files in os.walk(dirpath):
for name in files:
file_list.append(name)
file_list = sorted(file_list, key=getsize)
for item in file_list:
sys.stdout.write(str(file) + '\n')
else:
print "not found"
Can anyone point me in the right direction?
os.walk. It seems to be the right choice for dealing with directory trees. If you look at the example for this function, you will see, you are on a good way... - koffein