Get File size
Os.path.getsize (File_path): File_path for file path
Import os>>> os.path.getsize ('d:/svn/bin/scilexer.dll')1479904
Get folder Size
Traverse the folder to add all file sizes. Traversing folders using the Os.walk function
ImportOS fromOs.pathImportJoin, GetSizedefgetdirsize (dir): Size=0 forRoot, dirs, filesinchOs.walk (dir): Size+ = SUM ([GetSize (Join (root, name)) forNameinchfiles]) returnsizeif __name__=='__main__': Size= Getdirsize (r'D:\SVN') Print('there is%.3f'% (size/1024/1024),'Mbytes in D:\\svn') Execution Results:
Help (Os.walk) for information
Help on function Walkinchmodule Os:walk (top, Topdown=true, Onerror=none, followlinks=False) Directory Tree Generator. for each directoryinchthe directory tree rooted at top (including top itself, but excluding'.' and '..'), yields a 3-tuple Dirpath, dirnames, filenames Dirpath isA string, the path to the directory. Dirnames isa list of the names of the subdirectoriesinchDirpath (excluding'.' and '..'). Filenames isA list of the names of the non-directory filesinchDirpath. Note that the namesinchthe lists is just names, with no path components. To get a full path (which begins with top) to a fileorDirectoryinchDirpath, do Os.path.join (Dirpath, name). If Optional arg'Topdown' isTrueor notspecified, the triple fora directory isGenerated before the triples forAny of its subdirectories (directories is generated top down). If Topdown isfalse, the triple forA directory isGenerated after the triples forAll of its subdirectories (directories is generated bottom up). When Topdown isTrue, the caller can modify the Dirnames listinch-Place (e.g., viadel orSlice assignment), andWalk would recurse into the subdirectories whose names remaininchDirnames; This can is used to prune the search,orTo impose a specific order of visiting. modifying dirnames when Topdown isFalse isIneffective, since the directoriesinchDirnames has already been generated by the time Dirnames itself isgenerated. No matter the value of Topdown, the list of subdirectories isretrieved before the tuples forThe directory andIts subdirectories is generated. By default errors fromThe Os.scandir () call is ignored. If Optional arg'onerror' isspecified, it should be a function; it is called with one argument, an OSError instance. It can report the error toContinueWith the Walk,or RaiseThe exception to abort the walk. Note that the filename isavailable as the filename attribute of the exception object. By default, Os.walk does notfollow symbolic links to subdirectories in systems that support them. In order to get this functionality, set the optional argument'followlinks'To true. Caution:ifYouPassA relative pathname forTop, Don.'T Change theCurrent working directory between resumptions of walk. Walk never changes the current directory, andAssumes that the client doesn'Teither. Example:ImportOS fromOs.pathImportJoin, GetSize forRoot, dirs, filesinchOs.walk ('Python/lib/email'): Print(Root,"consumes", end="") Print(Sum ([GetSize (Join (root, name)) forNameinchFiles]), end="") Print("bytes in", Len (Files),"non-directory Files") if 'CVS' inchDirs:dirs.remove ('CVS')#don ' t visit CVS directories
Python3 get file and folder size