Shoham
Shoham

Reputation: 7294

List all files in a directory - filter by size

Im trying to to get all files which are larger then 20kb.
I tried the following but it returns all files, even when they are smaller then 20kb:

dir = 'C:\\some\\path'
filter(lambda x:os.path.getsize>20000L, [os.path.join(dir, x) for x in os.listdir(dir)])

Upvotes: 2

Views: 4242

Answers (3)

Sinux
Sinux

Reputation: 1808

To get all files larger than 20000

>>>import os
>>>dir = 'C:\\some\\path'
>>>[(path +file) for file in os.listdir(dir) if os.path.getsize(file) > 20000]

Upvotes: 1

philshem
philshem

Reputation: 25331

To print files that are larger than 20kb:

import glob
import os

for fyle in glob.glob('*'):
    if os.stat(fyle).st_size > 20000:
        print fyle, os.stat(fyle).st_size

Upvotes: 2

bereal
bereal

Reputation: 34272

The expression inside the lambda is not using its argument. In fact,

lambda x: os.path.getsize > 20000

compares the function getsize to an integer. That should be:

lambda x: os.path.getsize(x) > 20000

Upvotes: 5

Related Questions