i.darker
i.darker

Reputation: 29

finding total file size of all files in a directory

So currently I'm using the code seen below to calculate the size of all the files in a directory. this works fine for my uses. just wondered if there was a way to make it get the total file size of specific file types such as .py files or .txt.

total_file = sum(os.path.getsize(f) for f in os.listdir('.') if os.path.isfile(f))

Upvotes: 1

Views: 884

Answers (1)

fferri
fferri

Reputation: 18940

Use str.endswith('.py') (for filtering for .py files):

total_file = sum(os.path.getsize(f) for f in os.listdir('.')
    if os.path.isfile(f) and f.endswith('.py'))

Upvotes: 4

Related Questions