Reputation: 29
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
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