0

im wanting to sort files by size and pick the top 20
i have managed this with the glob.glob statement but it does not work quickly and does not pick up if a file is modified/ added while the code is running.
i have worked out how to do this with OS.walk

BUT I CANNOT GET IT TO FIND THE FILE SIZE
\/\/ my code to get size of files?

import os
folder = os.walk('.')
mylist=[]

for files in folder:
  size = os.path.getsize(files)
  print (str(size))

my error message
\/\/

Traceback (most recent call last):
  File "C:\Documents and Settings\Administrator\My Documents\pyfiles\test111.py", line 6, in <module>
    size = os.path.getsize(files)
  File "C:\Python31\lib\genericpath.py", line 49, in getsize
    return os.stat(filename).st_size
TypeError: Can't convert 'tuple' object to str implicitly

any help with that would be great as cant seem to find much on getting size of a file on google.
thanks

Edited by danholding: forgot a bi of code

2
Contributors
1
Reply
4
Views
6 Years
Discussion Span
Last Post by Gribouillis
0

READ THE DOC!
os.walk returns a sequence of triples (dirpath, dirnames, filenames)

suggestion: install pydoc. You only need to type pydoc os.walk in a terminal (cmd shell for windows) to see the doc.

Edited by Gribouillis: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.