How python3 looks at folder size
This article mainly introduces the relevant knowledge of "python3 how to view folder size". Xiaobian shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope this article "python3 how to view folder size" can help you solve the problem.
Specific operation method:
1. First, you need to import the os module into the python script for file operations.
import os
2, and then traverse all the contents of the folder by using the os.walk function.
3. After traversing the folder, you can sum all file sizes statistically.
The code example is as follows:
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
if __name__ == '__main__':
size = getdirsize(r'D:\yisu')
print('There are %.3f' % (size / 1024 / 1024), 'Mbytes in D:\\yisu')\
Related operations:
Use the os.path.getsize ('file path') method to get the file size.
import os #import os module
os.path.getsize('d:/bin/yisu.txt ') #Get the specified file size
1329925
About "python3 how to view folder size" content introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the industry information channel. Xiaobian will update different knowledge points for you every day.