python file extensions
Hi everybody,
I need help about my code, i need to find number of files and size grouped by extension. But to print like this: 2 345kb jpeg Right now it prints like this For extension: .docx number of files is: 1 item(s) For extension: .jpg number of files is: 2 item(s) For extension: .docx total file size is: 12581 size For extension: .jpg total file size is: 1466829 size
How could I turn the size of file from bytes in kb? Thanks in advance
import os
import collections
extensions = collections.defaultdict(int)
size = collections.defaultdict(int)
folderPath = 'C:\Users\Bojana\.PyCharmCE2017.2\config\scratches\Task4'
for path, dirs, files in os.walk(folderPath):
for file in files:
extensions[os.path.splitext(file)[1].lower()] += 1
size[os.path.splitext(file)[1].lower()] += os.path.getsize(path+os.path.sep+file)
for key,value in extensions.items():
print 'For extension: ', key, ' number of files is:', value, ' item(s)'
for key,value in size.items():
print 'For extension: ', key, 'total file size is:', value, ' size'
• 777 views
•
link
0 answers
No answers yet.
Log in to answer this question.
Hello bojanas93!
We believe that this post does not fit the main topic of this site.
This is unrelated to bioinformatics
For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.
If you disagree please tell us why in a reply below, we'll be happy to talk about it.
Cheers!