简体   繁体   中英

Python vs. Windows: File size calculator discrepancy

I recently made a simple file size calculator using os.walk() in python as follows:

import os

totalSize = 0
for root, dirs, files in os.walk("F:\\Japan"):
    for name in files:
        totalSize += os.path.getsize(os.path.join(root, name))
    for name in dirs:
        totalSize += os.path.getsize(os.path.join(root, name))

print(totalSize)

When I execute this code the output is 7,731,584,492 bytes, but when I analyze the same directory with right click > properties the file size is 7,731,486,188 bytes. Now, I know that a discrepancy of 98,304 bytes isn't much, but I would like to know if there is either a problem with my code or a more fundamental reason to this discrepancy.

Thank you.

Given the following test on my own machine:

>>> os.path.getsize(r'F:\My Music\Audio')
98304

I would say that the Windows size calculation does not include the space taken up by the file that describes a directory itself, while your code explicitly does (with the second inner for loop). The file for the particular directory you are inspecting happens to have that size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM