1. Os.path.getsize can get the file size
Import osfile_name = ' XXXXXX '
Print (Os.path.getsize (file_name))
2, get the folder size, that is, traverse the folder, all the file size plus. Traversing folders using the Os.walk function
Os.walk () can get a ternary tupple (Dirpath, Dirnames, filenames),
1, the first one is the starting path,
2. The second is a folder under the starting path,
3. The third one is the file under the starting path.
Where Dirpath is a string that represents the path to the directory, Dirnames is a list that contains the names of all subdirectories under Dirpath. Filenames is a list that contains the name of a non-directory file. These names do not contain path information, and you need to use Os.path.join (Dirpath, name) if you need to get the full path.
Import OS
From Os.path import join, GetSize
def getdirsize (dir):
Size = 0
For root, dirs, files in Os.walk (dir):
Size + = SUM ([GetSize (Join (root, name)) for name in Files])
return size
Python get file and folder size