How do I open files to read/write in Mojo?

I want to do the equivalent of the following Python code:
with open(file.txt, 'r') as file:
x = file.read()
with open(file.txt, 'r') as file:
x = file.read()
How can I do this?
23 Replies
sa-code
sa-code14mo ago
You can use @lukas 's lib for now: https://github.com/lsh/shims
GitHub
GitHub - lsh/shims: Utils for mojo projects
Utils for mojo projects. Contribute to lsh/shims development by creating an account on GitHub.
bhavnicksm
bhavnicksmOP14mo ago
@sa-code How do you use this repository to read files? The read has code but there's no example usage, so it's hard to understand
lukas
lukas14mo ago
The read code has an example at the top of the file
bhavnicksm
bhavnicksmOP14mo ago
Thanks, will try it out!
Tenesis
Tenesis14mo ago
@lukas can you give an example on how to write files as well? Thanks a lot!
lukas
lukas14mo ago
I haven’t written a wrapper for that yet I haven’t had a need, but will probably get around to it soon
Tenesis
Tenesis14mo ago
I see. So as far as we know there's currently no way to write to file in Mojo yet?
lukas
lukas14mo ago
You can call the CAPIs i.e write your own fwrite wrapper in the same way I did an fread wrapper
Tenesis
Tenesis14mo ago
Cool, I can try that out Thanks!
Tenesis
Tenesis14mo ago
GitHub
GitHub - ShuzhaoFeng/mojo-minwa: The minimal library you need to wr...
The minimal library you need to write / append file in Mojo - GitHub - ShuzhaoFeng/mojo-minwa: The minimal library you need to write / append file in Mojo
lukas
lukas14mo ago
Nice 🙂 @Tenesis thought I'd ping you that I've since added a buffered writer
Tenesis
Tenesis14mo ago
Awesome. 🥳 Thanks Lukas
lukas
lukas14mo ago
By the way there’s now the open method available, but I’m pretty sure it’s not buffered yet, so its use will depend on what you’re doing
Elaid Tebabkha
Elaid Tebabkha14mo ago
It is simple with the version 0.4.0 , you have to see the documention of this version 🙏:mojo: :python:
bhavnicksm
bhavnicksmOP14mo ago
Thanks @Elaid Tebabkha! 🩵 Will check it out now
ModularBot
ModularBot14mo ago
Congrats @bhavnicksm, you just advanced to level 1!
bhavnicksm
bhavnicksmOP14mo ago
@Elaid Tebabkha @lukas I was trying out the open method yesterday and I am facing issues with both reading and writing to a file. Mojo v0.4 doesn't seem to be able to parse it properly. Here's a screenshot of what I faced, and the code I was trying to execute
No description
No description
Balderdash
Balderdash14mo ago
from pathlib.path import cwd
from pathlib.path import Path

from python import Python

fn main() raises:
let currentPath = cwd()
let file = currentPath/"my_file.txt"

with open(file, "w") as f:
f.write("Hello, world!")

with open(file, "r") as f:
print(f.read())

let os = Python.import_module("os")
os.remove(file.path)
from pathlib.path import cwd
from pathlib.path import Path

from python import Python

fn main() raises:
let currentPath = cwd()
let file = currentPath/"my_file.txt"

with open(file, "w") as f:
f.write("Hello, world!")

with open(file, "r") as f:
print(f.read())

let os = Python.import_module("os")
os.remove(file.path)
It's a working example .. you may add raises to your main function ?
bhavnicksm
bhavnicksmOP14mo ago
@Balderdash Oh with raises it seems to work! Thanks Do you know why this works though? I don’t understand this properly
Balderdash
Balderdash14mo ago
When you call a function, which throws an exception you need either to catch and handle it or define in your function by using key 'raises' its given to its caller to handle it.
Elaid Tebabkha
Elaid Tebabkha14mo ago
Or use try except finally.🙏
bhavnicksm
bhavnicksmOP14mo ago
Yes, try-except also seems to work
Elaid Tebabkha
Elaid Tebabkha14mo ago
You can also write The code in python an import it inside the Mojo code like a PythonObject and execute the mojo code .
Want results from more Discord servers?
Add your server