Created
January 27, 2012 18:15
-
-
Save nitinsatish/1690114 to your computer and use it in GitHub Desktop.
Find the largest file of given extension in a directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os, sys | |
if len(sys.argv)==1 : | |
folder='.' | |
else : | |
folder=sys.argv[1] | |
if len(sys.argv)==3 : | |
extention=sys.argv[2] | |
else : | |
extention='py' | |
filez=os.listdir(folder) | |
pfiles=[] | |
for fil in filez : | |
if fil.endswith(extention) : | |
pfiles.append(fil) | |
print '\n Files matching extension \n' | |
print pfiles | |
fil_size=[] | |
for pfil in pfiles : | |
fpath=os.path.join(folder,pfil) | |
fsize=os.path.getsize(fpath) | |
fil_size.append((pfil,fsize)) | |
def getfilsiz(tupl): | |
return tupl[1] | |
sorted_fil=sorted(fil_size,key=getfilsiz) | |
print ('\n = = = = = = \n') | |
print 'Largest file : ', sorted_fil[-1][0] | |
if sorted_fil[-1][1] > 1048576: | |
print 'Size : ', sorted_fil[-1][1]/1048576,'MB' | |
elif sorted_fil[-1][1] >1024 : | |
print 'Size : ', sorted_fil[-1][1]/1024 ,'KB' | |
else : | |
print 'Largest file : ', sorted_fil[-1][1],'Bytes' | |
print ' ' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment