Find largest file in directory

Thread Solved

Join Date: Oct 2009
Posts: 2
Reputation: keyoh is an unknown quantity at this point 
Solved Threads: 0
keyoh keyoh is offline Offline
Newbie Poster

Find largest file in directory

 
0
  #1
27 Days Ago
I'm working on a cleanup script for tv shows that I download. Right now I'm just looking for a file greater than 50mb, but there should be a better way.

  1. import os
  2. import shutil
  3.  
  4. dir = "C:\Users\Bobe\Downloads\TV\\"
  5.  
  6. for folder in os.listdir(dir):
  7. if os.path.isdir(os.path.join(dir,folder)):
  8. for file in os.listdir(dir + folder):
  9. filelocation = dir+folder+"\\"+file
  10. if os.path.getsize(filelocation) > 50000000:
  11. shutil.move(filelocation, dir + folder + ".avi")
  12. else:
  13. os.remove(filelocation)
  14.  
  15. shutil.rmtree(dir + folder)
Last edited by keyoh; 27 Days Ago at 3:38 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,972
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 920
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #2
25 Days Ago
What is your problem with the code you have?
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: keyoh is an unknown quantity at this point 
Solved Threads: 0
keyoh keyoh is offline Offline
Newbie Poster
 
0
  #3
25 Days Ago
I feel finding a file greater than 50mb is kind of a hack fix. Is there a simple way to return the filename of the largest file in a directory?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 140
Reputation: mn_kthompson is an unknown quantity at this point 
Solved Threads: 29
mn_kthompson mn_kthompson is offline Offline
Junior Poster
 
0
  #4
25 Days Ago
OK, check this out. The code you're using to find files is pretty good. If you're looking to improve that part of the code you could use recursion so that your function will descend into subdirectories and pull out those files too. Here is some code I whipped up that will print the number of files in all the subdirectories under a folder.
  1. import os
  2.  
  3. # Windows and linux slashes go in opposite directions.
  4. # Uncomment the slash appropriate for your system.
  5. systemslash='/'
  6. # systemslash='\'
  7.  
  8. def get_list_of_files(inDirectory, container=[]):
  9. for entry in os.listdir(inDirectory):
  10. if os.path.isdir(inDirectory+systemslash+entry):
  11. get_list_of_files(inDirectory+systemslash+entry,container)
  12. container.append(inDirectory+systemslash+entry)
  13. return container
  14.  
  15. Final_List_of_Files = get_list_of_files('/Users/kevin')
  16. print len(Final_List_of_Files)

Now you want to get the list of files and the file size, so I would suggest putting them into a list of tuples which you can sort to get the biggest file of them all. Change the line where we add the file name to the list so that it adds a tuple containing the file name and size.
  1. filesize = os.path.getsize(inDirectory+systemslash+entry)
  2. fileandsize = (filesize, inDirectory+systemslash+entry)
  3. container.append(fileandsize)

Then your last task is to sort the list of tuples with Final_List_of_Files.sort(). You'll have to reverse the sort order so that you can the largest file in the top position. Here is the final code
  1. import os
  2.  
  3. # Windows and linux slashes go in opposite directions.
  4. # Uncomment the slash appropriate for your system.
  5. systemslash='/'
  6. # systemslash='\'
  7.  
  8. def get_list_of_files(inDirectory, container=[]):
  9. for entry in os.listdir(inDirectory):
  10. entry = inDirectory+systemslash+entry
  11. if os.path.isdir(entry):
  12. get_list_of_files(entry,container)
  13. filesize = os.path.getsize(entry)
  14. fileandsize = (filesize, entry)
  15. container.append(fileandsize)
  16. return container
  17.  
  18. Final_List_of_Files = get_list_of_files('/Users/kevin/Documents')
  19. Final_List_of_Files.sort(reverse=True)
  20.  
  21. print Final_List_of_Files[0]
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,273
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven
 
0
  #5
25 Days Ago
Well, you could do something like this:
  1. # File_lister2.py
  2. # create a list of all the files and sizes in a given direcory
  3. # and optionally any of its subdirectories (Python2 & Python3)
  4. # snee
  5.  
  6. import os
  7.  
  8. def file_lister(directory, subs=False):
  9. """
  10. returns a list of (size, full_name) tuples of all files
  11. in a given directory
  12. if subs=True also any of its subdirectories
  13. """
  14. mylist = []
  15. for fname in os.listdir(directory):
  16. # add directory to filename for a full pathname
  17. full_name = os.path.join(directory, fname)
  18. # size in kb
  19. size = int(os.path.getsize(full_name)//1024) + 1
  20. if not os.path.isdir(full_name):
  21. # append a (size, full_name) tuple
  22. mylist.append((size, full_name))
  23. elif subs==True:
  24. # optionally recurse into subdirs
  25. file_lister(full_name)
  26. return mylist
  27.  
  28. #dir_name = r"C:\Python31\Tools" # Windows
  29. dir_name = "/home/dell/Downloads" # Linux
  30. file_list = file_lister(dir_name)
  31.  
  32. # show the list sorted by size
  33. for file_info in sorted(file_list, reverse=True):
  34. print(file_info)
  35.  
  36. print('-'*66)
  37.  
  38. print( "The largest file is: \n%s (%skb)" % \
  39. (max(file_list)[1], max(file_list)[0]) )
  40.  
  41. """a typical partial output -->
  42. (24144, '/home/dell/Downloads/ActivePython-2.6.2.2-linux-x86.tar.gz')
  43. (23320, '/home/dell/Downloads/ActivePython-3.1.0.1-linux-x86.tar.gz')
  44. (9288, '/home/dell/Downloads/Python-3.1.tar.bz2')
  45. ...
  46. ...
  47. ------------------------------------------------------------------
  48. The largest file is:
  49. /home/dell/Downloads/ActivePython-2.6.2.2-linux-x86.tar.gz (24144kb)
  50. """
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 77
Reputation: pythopian is an unknown quantity at this point 
Solved Threads: 18
pythopian pythopian is offline Offline
Junior Poster in Training
 
1
  #6
24 Days Ago
Arrr, too much code noise! ;-)

If you know you have at least one file:
  1. import os, glob
  2. largest = sorted( (os.path.getsize(s), s) for s in glob.glob('yourdir/*.avi') )[-1][1]

If not, you split the code a bit:
  1. import os, glob
  2. files = glob.glob('yourdir/*.avi')
  3. largest = sorted((os.path.getsize(s), s) for s in files)[-1][1] if files else ''
  4. if largest:
  5. ... # do something with it
Last edited by pythopian; 24 Days Ago at 7:37 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC