Title: Report actual size from 'os.path.getsize'
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.10
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List:, stephenfin
Priority: normal Keywords: patch

Created on 2020-06-23 16:44 by stephenfin, last changed 2020-06-26 07:03 by

Pull Requests
URL Status Linked Edit
PR 21088 open stephenfin, 2020-06-23 16:53
Messages (2)
msg372183 - (view) Author: Stephen Finucane (stephenfin) * Date: 2020-06-23 16:44
The 'os.path.getsize' API returns the apparent size of the file at *path*, that is, the number of bytes the file reports itself as consuming. However, it's often useful to get the actual size of the file, or the size of file on disk. It would be helpful if one could get this same information from 'os.path.getsize'.
msg372412 - (view) Author: Gareth Rees ( * (Python triager) Date: 2020-06-26 07:03
The proposed change adds a Boolean flag to os.path.getsize() so that it returns:

    os.stat(filename).st_blocks * 512

(where the 512 is the file system block size on Linux; some work is needed to make this portable to other operating systems).

The Boolean argument here would always be constant in practice -- that is, you'd always call it like this:

    virtual_size = os.path.getsize(filename, apparent=True)
    allocated_size = os.path.getsize(filename, apparent=False)

and never like this:

    x_size = os.path.getsize(filename, apparent=x)

where x varies at runtime.

The "no constant bool arguments" design principle [1] suggests that this should be added as a new function, something like os.path.getallocatedsize().

Date User Action Args
2020-06-26 07:03:47gdr@garethrees.orgsetnosy: +
messages: + msg372412
2020-06-23 16:53:38stephenfinsetkeywords: + patch
stage: patch review
pull_requests: + pull_request20254
2020-06-23 16:44:46stephenfincreate