Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 882
UserYmY Avatar asked Dec 02 '22 21:12

UserYmY


1 Answers

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)
like image 92
Steinar Lima Avatar answered Dec 05 '22 09:12

Steinar Lima