Calculating size folder python [closed]

2024/11/18 7:29:59

I want to ask how to calculate 3 folder size together using python and show as dataframe.

I have folder: A B C

The output that i want is:

folder        size
A            .... byte
B            .... byte
C            .... byte
Answer

Try the below code-

import os
f= r"D:\Reg"
folder_name= os.path.basename(f)
def get_size(start_path = '.'):total_size = 0for dirpath, dirnames, filenames in os.walk(start_path):for f in filenames:fp = os.path.join(dirpath, f)total_size += os.path.getsize(fp)return total_size
print "{0:<20}      {1:>0}".format("Folder"," Size")
print "{0:<20}        {1:>0}".format(folder_name,str(get_size(f))+" bytes")

You can use looping for folder folders = [r"D:\A",r"D:\B",r"D:\C"] as for i in folders do that!

https://en.xdnf.cn/q/120101.html

Related Q&A

Answer Error, Only outputting Zero

I am coding in python and I cannot seem to figure out why when the amount of tickets sold is entered it does not calculate the full price of the tickets that were sold. Any help is appreciated, Thanks.…

finding a pattern match and concatenating the rest of lines in python

I have a small data set to clean. I have opened the text file in Pycharm. The data set is like this:Code-6667+ Name of xyz company+ Address + Number+ Contact person+ Code-6668+ Name of abc company, A…

Flat a list of containing only one dict

I have a dict which contain a list product which will contain only one dict: d = {"thickness": 90.0,"mass_surf": 37.8,"res_therm": 0.75,"codename": "codenam…

How to breakup a list of list in a given way in Python [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Incrementing a counter while assigning it in python

Is it possible to do the following in python somehow?nodename = node%s % (++num if ct[0] != J else num)Or do I need to first do the increment and the assign it on the next line:if ct[0] != J:num += 1n…

Append two arrays together into one? (Numpy/Python)

I currently have an array of strings and Im trying to join it together with another array of strings to form a complete word to do some web parsing. For example:`Var1 [A B C, .... ....] Var2 …

Python 2.7 Isolate multiple JSON objects in a string

Ive to parse a text file that contains different kind of data. The most challenging is a line that contains three different JSON object (strings) and other data between them. Ive to divide the Json da…

pass different C functions with pointer arrays as the function argument to a class

I am trying to pass different functions which have pointers as arguments to a python function. One example of the input function as input parameter is the given normal function: Sample.pyxfrom cpython …

dynamic filter choice field in django

I am using django-filter to filter results on a page. I am able to use static choice filters like this:filters.pyFILTER_CHOICES = ( (, All States), (AL, Alabama), (AK, Alaska), (AZ, Arizona), #and so f…

How to print a table from a text file and fill empty spaces?

I have the following table in a txt file:|Client | Container weight | Country of Origin | Product code ||S4378 | 450 Ton | China | 457841 || | 350 Ton | Japan…