| | |
Find largest file in directory
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 2
Reputation:
Solved Threads: 0
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.
Python Syntax (Toggle Plain Text)
import os import shutil dir = "C:\Users\Bobe\Downloads\TV\\" for folder in os.listdir(dir): if os.path.isdir(os.path.join(dir,folder)): for file in os.listdir(dir + folder): filelocation = dir+folder+"\\"+file if os.path.getsize(filelocation) > 50000000: shutil.move(filelocation, dir + folder + ".avi") else: os.remove(filelocation) shutil.rmtree(dir + folder)
Last edited by keyoh; 28 Days Ago at 3:38 pm.
•
•
Join Date: Nov 2007
Posts: 141
Reputation:
Solved Threads: 31
0
#4 26 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.
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.
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
python Syntax (Toggle Plain Text)
import os # Windows and linux slashes go in opposite directions. # Uncomment the slash appropriate for your system. systemslash='/' # systemslash='\' def get_list_of_files(inDirectory, container=[]): for entry in os.listdir(inDirectory): if os.path.isdir(inDirectory+systemslash+entry): get_list_of_files(inDirectory+systemslash+entry,container) container.append(inDirectory+systemslash+entry) return container Final_List_of_Files = get_list_of_files('/Users/kevin') 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.
python Syntax (Toggle Plain Text)
filesize = os.path.getsize(inDirectory+systemslash+entry) fileandsize = (filesize, inDirectory+systemslash+entry) 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
python Syntax (Toggle Plain Text)
import os # Windows and linux slashes go in opposite directions. # Uncomment the slash appropriate for your system. systemslash='/' # systemslash='\' def get_list_of_files(inDirectory, container=[]): for entry in os.listdir(inDirectory): entry = inDirectory+systemslash+entry if os.path.isdir(entry): get_list_of_files(entry,container) filesize = os.path.getsize(entry) fileandsize = (filesize, entry) container.append(fileandsize) return container Final_List_of_Files = get_list_of_files('/Users/kevin/Documents') Final_List_of_Files.sort(reverse=True) print Final_List_of_Files[0]
0
#5 26 Days Ago
Well, you could do something like this:
python Syntax (Toggle Plain Text)
# File_lister2.py # create a list of all the files and sizes in a given direcory # and optionally any of its subdirectories (Python2 & Python3) # snee import os def file_lister(directory, subs=False): """ returns a list of (size, full_name) tuples of all files in a given directory if subs=True also any of its subdirectories """ mylist = [] for fname in os.listdir(directory): # add directory to filename for a full pathname full_name = os.path.join(directory, fname) # size in kb size = int(os.path.getsize(full_name)//1024) + 1 if not os.path.isdir(full_name): # append a (size, full_name) tuple mylist.append((size, full_name)) elif subs==True: # optionally recurse into subdirs file_lister(full_name) return mylist #dir_name = r"C:\Python31\Tools" # Windows dir_name = "/home/dell/Downloads" # Linux file_list = file_lister(dir_name) # show the list sorted by size for file_info in sorted(file_list, reverse=True): print(file_info) print('-'*66) print( "The largest file is: \n%s (%skb)" % \ (max(file_list)[1], max(file_list)[0]) ) """a typical partial output --> (24144, '/home/dell/Downloads/ActivePython-2.6.2.2-linux-x86.tar.gz') (23320, '/home/dell/Downloads/ActivePython-3.1.0.1-linux-x86.tar.gz') (9288, '/home/dell/Downloads/Python-3.1.tar.bz2') ... ... ------------------------------------------------------------------ The largest file is: /home/dell/Downloads/ActivePython-2.6.2.2-linux-x86.tar.gz (24144kb) """
No one died when Clinton lied.
•
•
Join Date: Nov 2009
Posts: 78
Reputation:
Solved Threads: 20
1
#6 24 Days Ago
Arrr, too much code noise! ;-)
If you know you have at least one file:
If not, you split the code a bit:
If you know you have at least one file:
Python Syntax (Toggle Plain Text)
import os, glob largest = sorted( (os.path.getsize(s), s) for s in glob.glob('yourdir/*.avi') )[-1][1]
If not, you split the code a bit:
Python Syntax (Toggle Plain Text)
import os, glob files = glob.glob('yourdir/*.avi') largest = sorted((os.path.getsize(s), s) for s in files)[-1][1] if files else '' if largest: ... # do something with it
Last edited by pythopian; 24 Days Ago at 7:37 pm.
![]() |
Similar Threads
- No such file or directory (C)
- Using recursion to find largest item in an array (C++)
- plz help me in solving this files concept question (Java)
- Find word in file!! (C++)
- I get the error no file or directory when i try to run a perl script (Perl)
- HTTP Error 404 - File or directory not found. (ASP.NET)
- about:blank hijack (can't find any .dll-file) (Viruses, Spyware and other Nasties)
Other Threads in the Python Forum
- Previous Thread: How to generate HTML pages from a text file using python??
- Next Thread: wxPython text overwrite problem...
| Thread Tools | Search this Thread |
abrupt ansi anti apache approximation array assignment avogadro backend beginner binary bluetooth book builtin calculator character code converter countpasswordentry curved customdialog dan08 dictionaries dictionary dynamic examples exe file float format function gnu graphics gui heads homework ideas import inches input java launcher library line lines linux list lists loop mouse mysqlquery number numbers numeric output parsing path phonebook plugin pointer port prime programming progressbar projects py2exe pygame python random recursion redirect scrolledtext software statictext statistics string strings sum table terminal text textarea thread threading time tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable wordgame write wxpython xlib






