Get the size of a folder in Python - CodeSpeedy
文章推薦指數: 80 %
How to get the size of a folder in Python? · os.path.join() method joins different path components such as directories, sub directories, files, etc. Each ... HowtogetthesizeofafolderinPython?ByAsmaKhanInthistutorial,wewilllearnhowtogetthesizeofafolderinPython.Letusfirstseesomeprerequisiterequiredforthiscode.TheOSmoduleinPythonenablesustointeractwiththeoperatingsystemandperformvariousrelatedtasks.ThismoduleisapredefinedstandardmoduleofPython.os.pathmoduleisasubmoduleoftheOSmodule.Itcontainsvariousfunctionsonpathnamesorinsimplerterms,usedinsteadoflongfilenames.Toaccessthismodule,weusethesyntax:importosSomefunctionsofos.pathwewillneedtosolvethisproblemare:os.path.join()methodjoinsdifferentpathcomponentssuchasdirectories,subdirectories,files,etc.Eachcomponentisseparatedby(\).Syntax:os.path.join(path)os.path.getsize()methodreturnsthesizeofthefileordirectoryinbytes.Syntax:os.path.getsize(path)Anothermethodthatwillbeusedisos.walk().Itwalksthetreeofdirectorieseithertop-downorbottom-upandgeneratesthefilenames.3tuplesareyielded:dirpath,dirnames,filenames.PythonprogramtogeneratesizeofafolderProblemstatement: WriteaPythonprogramtogetthesizeofafolder.Steps/Algorithm: ImporttheOSmodule.Defineafunction(size)andgivethepathofthefolderordirectory.Initializethetotalsizeas0.Navigateorwalkthroughthedirectorytreeusingthe os.walk() method.Joinorconcatenateallthecomponentsofthepathusingthe os.path.join() function.Getthesizesofindividualfilesinthedirectoryandaddittothetotalsize.Printthetotalsizegenerated.Program/Code:importos defsize(path='C:\\Users\ADMIN\Documents\programs\Python'): #initializethesize total_size=0 #usethewalk()methodtonavigatethroughdirectorytree fordirpath,dirnames,filenamesinos.walk(path): foriinfilenames: #usejointoconcatenateallthecomponentsofpath f=os.path.join(dirpath,i) #usegetsizetogeneratesizeinbytesandaddittothetotalsize total_size+=os.path.getsize(f) returntotal_size print(size())IfwerunourprogramthenitwillshowthesizeofourgivenfileinthePythonprogram:1246Ihope,youhaveunderstoodhowwecangetthesizeofacompletefolderwithjustafewlinesofcode.Also,read:CountthenumberoffilesinadirectoryinPythonHowtogetthesizeofafileinPython LeaveaReplyCancelreplyYouremailaddresswillnotbepublished.Requiredfieldsaremarked*Comment*Name*Email*«FindthenextgreaternumberfromthesamesetofdigitsinC++StringPalindromeinJava»SearchLatestArticlesGlyphiconsinBootstrapRemoveduplicateelementsfromanarrayinSwiftEasyexampleofopenpyxliter_rowsinPythonHowtofindindexofmaximumandminimumelementinvectorinC++DictionariesinSwiftRelatedPostsHowtoremoveallemptyfileswithinafolderanditssubfoldersinPython?CountthenumberoffilesinadirectoryinPython.HowtogettheparentofthecurrentdirectoryinPython
延伸文章資訊
- 1how to get size of a folder or file in python Code Example
“how to get size of a folder or file in python” Code Answer ; 1. import os ; 2. ; 3. def get_si...
- 2How to Check File and Folder Size in Python? - Geekflare
In this article, you'll learn to check the size of a file or folder in Python Python is one of th...
- 3How to calculate the size of a directory in Python - Adam Smith
Call os.walk(path) with path as the directory to calculate the size of to return a generator of a...
- 4Python Count Number of Files in a Directory [4 Ways] - PYnative
- 5How to Get the Size of Directories in Python
Have you ever wondered how you can get folder size in bytes using Python? As you may already know...