Skip to content

Commit

Permalink
[stdlib] implement os.path.getsize
Browse files Browse the repository at this point in the history
Signed-off-by: Artemio Garza Reyna <artemiogr97@gmail.com>
  • Loading branch information
artemiogr97 committed May 18, 2024
1 parent 7b0fc31 commit f870698
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,9 @@ what we publish.
- Added `atof()` function which can convert a `String` to a `float64`.
([PR #2649](https://github.com/modularml/mojo/pull/2649) by [@fknfilewalker](https://github.com/fknfilewalker))

- 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
33 changes: 33 additions & 0 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 @@ -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 a 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 builtin._location import __source_location

from testing import assert_equal, assert_false


fn main() raises:
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 f870698

Please sign in to comment.