How to open a file in mojo? #732
Replies: 4 comments 5 replies
-
I don't believe there is a way to do this through any mojo library. but you can do it through python
Since I don't believe mojo supports Context Managers yet, you will need to call the |
Beta Was this translation helpful? Give feedback.
-
as for v0.6.1, you can: import pathlib
fn main():
let p: Path = Path("./test.txt")
try:
let str : String = p.read_text()
print(str)
finally:
pass |
Beta Was this translation helpful? Give feedback.
-
you can open a file with fn main() raises:
with open("file", "r") as file:
print(file.read()) |
Beta Was this translation helpful? Give feedback.
-
Thanks everyone very much for your solution, which can temporarily solve my problem of reading small files. For large files, I expect to read and process them line by line by reading until the \n character is encountered, instead of reading the entire file at once. |
Beta Was this translation helpful? Give feedback.
-
I am trying to read a file using mojo but didn't find any example in the getting started documentation
Beta Was this translation helpful? Give feedback.
All reactions