UserYmY
UserYmY

Reputation: 8574

How to check size of the files in a directory with python?

I have a folder that contains 4 text files. I want to program a code with which I would be able to check the size of the files in my folder and only open those that has equal sizes. Anyone has any idea?

I have already tried this

import os


d=os.stat('H:/My Documents/211').st_size

Upvotes: 4

Views: 12669

Answers (2)

babbageclunk
babbageclunk

Reputation: 8751

To get all of the files in a directory, you can use os.listdir.

>>> import os
>>> basedir = 'tmp/example'
>>> names = os.listdir(basedir)
>>> names
['a', 'b', 'c']

Then you need to add basedir on to the names:

>>> paths = [os.path.join(basedir, name) for name in names]
>>> paths
['tmp/example/a', 'tmp/example/b', 'tmp/example/c']

Then you can turn that into a list of pairs of (name, size) using a os.stat(path).st_size (the example files I've created are empty):

>>> sizes = [(path, os.stat(path).st_size) for path in paths]
>>> sizes
[('tmp/example/a', 0), ('tmp/example/b', 0), ('tmp/example/c', 0)]

Then you can group the paths with the same size together by using a collections.defaultdict:

>>> import collections
>>> grouped = collections.defaultdict(list)
>>> for path, size in sizes:
...     grouped[size].append(path)
... 
>>> grouped
defaultdict(<type 'list'>, {0: ['tmp/example/a', 'tmp/example/b', 'tmp/example/c']})

Now you can get all of the files by size, and open them all (don't forget to close them afterwards!):

>>> open_files = [open(path) for path in grouped[0]]

Upvotes: 8

Steinar Lima
Steinar Lima

Reputation: 7821

I can't reproduce your error. This

import os
print os.path.getsize('mydata.csv')
print os.stat('mydata.csv').st_size

Yields

359415
359415

I'm guessing that the filename you provide is wrong. This will print the size of all files in a folder

my_dir = r'path/to/subdir/'

for f in os.listdir(my_dir):
    path = os.path.join(my_dir, f)
    if os.path.isfile(path):
        print os.path.getsize(path)

Upvotes: 9

Related Questions