How JustAnswer Works:
  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site. Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.
Ask Ingo U Your Own Question
Ingo U
Ingo U, Software Engineer
Category: Programming
Satisfied Customers: 755
Experience:  Over 25 years experience in software development. Expert in Microsoft .Net, C#, C++, VB, SQL
44076068
Type Your Programming Question Here...
Ingo U is online now
A new question is answered every 9 seconds

I would like to give a directory and then report on the extensions

This answer was rated:

I would like to give a directory and then report on the extensions within the directory.
List each type of extension, count of extension, and total file size of extensions.

Output can be simple (extension, count, total size) like this:

.xlsx 2 1000
.py 1 590


This is what I have written so far. It kind of works. The output I am getting is writing the total of all files for each type of extension.

import os, os.path, collections
extensionCount = collections.defaultdict(int)
os.chdir('Test') #change to a directory in CWD

for root, dirs, files in os.walk('.'):
for file in files:
base, ext = os.path.splitext(file)
extensionCount[ext.lower()] += 1
size = os.path.getsize(file)

for ext, count in sorted(extensionCount.items(), key=lambda x: x[1], reverse=True):
print(ext, count, size)

('.docx', 2, 592L)
('.py', 1, 592L)

#I have a directory called "test" with three files equalling 592 bytes, which is the total
#of the three files.

Hello,

I'm Lindie, and I’m a moderator for this topic.

We have been working with the professionals to try to help you with your question. Sometimes it may take a bit of time to find the right fit.

I was checking to see if you had already found your answer or if you still needing assistance from one of the professionals.

Please let me know if you wish to continue waiting or if you would like for us to close your question.

Also remember that JustAnswer has a multitude of categories to help you with all your needs from Health, Pets, Computers, Cars, Finance, Law, to Home Improvement, and more.

Thank you,

Lindie

Customer: replied 4 years ago.

Hi Lindie,


 


No answer yet. Thanks for checking in with me.

Customer: replied 4 years ago.

I just tried this as well. I almost have what I want. I am just missing how to combine the extensions and their file size.


 


I am getting results like this:


Extension .ico | Filesize 19790 bytes
Extension .ico | Filesize 19790 bytes
Extension .pyd | Filesize 132096 bytes
Extension .dll | Filesize 73216 bytes
Extension .pyd | Filesize 9728 bytes


 


I would like this to be more of a summary where the extension are listed only once with the file sizes summed. My script is below.


 


import os
import glob


os.chdir(r'C:\Python33\DLLs')



for filename in glob.glob ('*'):
filesize = os.path.getsize(filename)
extension = os.path.splitext(filename)[1]
print("Extension",extension,"|","Filesize", filesize,"bytes")

Hello,

Thank you, XXXXX XXXXX continue to look for a professional to assist you. Please let me know if I can be of any further assistance while you wait.

Best,

Lindie

Hi,
I'm working on a solution for you. Will get back to you ASAP.
Regards,
Ingo U
Here you go:

http://wikisend.com/download/446796/filesizes.py

Please let me know if I can be of further assistance.
Regards,
Ingo
Customer: replied 4 years ago.

Hello,


 


Thank you for responding. I also need to include a count of the particular extension. This is a pretty long post and perhaps this requirment was lost. It is in the first post of mine. Do you think you would be able to include a count for the extension as shown below - just a number at the end of the row?


 


Extension .dll | Total space used (NNN) NNN-NNNNbytes | 2

No problem but I'm not at a computer at the moment
Can it wait until the morning?
Customer: replied 4 years ago.


Awesome. Thank you. I am not in a super rush and I can wait. No problem. I am new to programming and just can't figure out how to get the the file size, extension list, and count all together.

 

Thanks.

OK, here it is:

http://wikisend.com/download/611086/filesizes.py

If you want, I could also give you min/max/average size numbers...
Customer: replied 4 years ago.


I did not think of min/max/average. I you could do that it would be great! That would be all I needed.

Check it out:

http://www.mediafire.com/?nhosru1obwas7wm

(wikisend is having issues)
Hope you like the cleaner formatting!
Ingo U, Software Engineer
Category: Programming
Satisfied Customers: 755
Experience: Over 25 years experience in software development. Expert in Microsoft .Net, C#, C++, VB, SQL
Ingo U and other Programming Specialists are ready to help you