Skip to content

Commit

Permalink
[External] [stdlib] Implement os.path.getsize (#40291)
Browse files Browse the repository at this point in the history
[External] [stdlib] Implement `os.path.getsize`

Add `getsize` method for getting the size (in bytes)
of a path.

Fixes modularml#1130

ORIGINAL_AUTHOR=artemiogr97
<57588855+artemiogr97@users.noreply.github.com>
PUBLIC_PR_LINK=modularml#2626

Co-authored-by: artemiogr97 <57588855+artemiogr97@users.noreply.github.com>
Closes modularml#2626
MODULAR_ORIG_COMMIT_REV_ID: 443260f3be31a694c9e93fc11b64d0317646c4e0
  • Loading branch information
2 people authored and martinvuyk committed May 24, 2024
1 parent 44e5f19 commit e0d76c3
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 6 deletions.
3 changes: 3 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,9 @@ what we publish.
print("x contains 1")
```

- Added `os.getsize` function, which gives the size in bytes of a path.
([PR 2626](https://github.com/modularml/mojo/pull/2626) by [@artemiogr97](https://github.com/artemiogr97))

### 🦋 Changed

- The `let` keyword has been completely removed from the language. We previously
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/os/path/__init__.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
# limitations under the License.
# ===----------------------------------------------------------------------=== #

from .path import exists, isdir, isfile, islink, lexists
from .path import exists, isdir, isfile, islink, lexists, getsize
43 changes: 38 additions & 5 deletions stdlib/src/os/path/path.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ from .._linux_x86 import _lstat as _lstat_linux_x86
from .._linux_x86 import _stat as _stat_linux_x86
from .._macos import _lstat as _lstat_macos
from .._macos import _stat as _stat_macos
from ..fstat import stat


# ===----------------------------------------------------------------------=== #
Expand Down Expand Up @@ -91,7 +92,7 @@ fn isdir[pathlike: os.PathLike](path: pathlike) -> Bool:
symbolic links, so both islink() and isdir() can be true for the same path.
Parameters:
pathlike: The a type conforming to the os.PathLike trait.
pathlike: The type conforming to the os.PathLike trait.
Args:
path: The path to the directory.
Expand Down Expand Up @@ -131,7 +132,7 @@ fn isfile[pathlike: os.PathLike](path: pathlike) -> Bool:
"""Test whether a path is a regular file.
Parameters:
pathlike: The a type conforming to the os.PathLike trait.
pathlike: The type conforming to the os.PathLike trait.
Args:
path: The path to the directory.
Expand Down Expand Up @@ -167,7 +168,7 @@ fn islink[pathlike: os.PathLike](path: pathlike) -> Bool:
symbolic link.
Parameters:
pathlike: The a type conforming to the os.PathLike trait.
pathlike: The type conforming to the os.PathLike trait.
Args:
path: The path to the directory.
Expand Down Expand Up @@ -204,7 +205,7 @@ fn exists[pathlike: os.PathLike](path: pathlike) -> Bool:
"""Return True if path exists.
Parameters:
pathlike: The a type conforming to the os.PathLike trait.
pathlike: The type conforming to the os.PathLike trait.
Args:
path: The path to the directory.
Expand Down Expand Up @@ -241,7 +242,7 @@ fn lexists[pathlike: os.PathLike](path: pathlike) -> Bool:
"""Return True if path exists or is a broken symlink.
Parameters:
pathlike: The a type conforming to the os.PathLike trait.
pathlike: The type conforming to the os.PathLike trait.
Args:
path: The path to the directory.
Expand All @@ -250,3 +251,35 @@ fn lexists[pathlike: os.PathLike](path: pathlike) -> Bool:
Returns True if the path exists or is a broken symbolic link.
"""
return exists(path.__fspath__())


# ===----------------------------------------------------------------------=== #
# getsize
# ===----------------------------------------------------------------------=== #


fn getsize(path: String) raises -> Int:
"""Return the size, in bytes, of the specified path.
Args:
path: The path to the file.
Returns:
The size of the path in bytes.
"""
return stat(path).st_size


fn getsize[pathlike: os.PathLike](path: pathlike) raises -> Int:
"""Return the size, in bytes, of the specified path.
Parameters:
pathlike: The type conforming to the os.PathLike trait.
Args:
path: The path to the file.
Returns:
The size of the path in bytes.
"""
return getsize(path.__fspath__())
31 changes: 31 additions & 0 deletions stdlib/test/os/path/test_getsize.mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# ===----------------------------------------------------------------------=== #
# Copyright (c) 2024, Modular Inc. All rights reserved.
#
# Licensed under the Apache License v2.0 with LLVM Exceptions:
# https://llvm.org/LICENSE.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===----------------------------------------------------------------------=== #
# RUN: %mojo %s

import os
from os.path import getsize

from testing import assert_equal, assert_false


fn main() raises:
# TODO: use `NamedTemporaryFile` once we implement it.
alias file_name = "test_file"
assert_false(os.path.exists(file_name), "File should not exist")
with open(file_name, "w"):
pass
assert_equal(getsize(file_name), 0)
with open(file_name, "w") as my_file:
my_file.write("test")
assert_equal(getsize(file_name), 4)
os.remove(file_name)

0 comments on commit e0d76c3

Please sign in to comment.