This returns the size. It adds a new tuple to the pairs list. Finally We call the sort method with an argument, a lambda expression that sorts based on a key. The key is the first element the size of each tuple. In the resulting list, the files are sorted from smallest to largest. All the script files in my directory were small, between bytes to bytes. The list can be reversed with the reverse method. Sorting can be simple, as shown on many websites, or complex.
Often elements must be sorted in an associative way. Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert. Writing code in comment? Please use ide. Load Comments. What's New. Most popular in Python. More related articles in Python. Check if element exists in list in Python Python program to check if a string is palindrome or not Programs for printing pyramid patterns in Python. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 8 years, 1 month ago. Active 2 months ago. Viewed 28k times. 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. I suggest to read the help for the function os.
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 I have to make sure: Do you want to sort files only of the provided directory or do you want to sort all files including the files in the subdirectory??
Add a comment. Active Oldest Votes. This is a approach using generators. This is pretty fast and is useful to get a quick idea of which files are taking up space. I haven't tested, but if a subdirectory contains other files and directories you don't have access to, most likely the sort is going to fail, because os.
Hopefully this function will help you out I'm using Python 2. Owens J.
0コメント