How do I open files to read/write in Mojo?
I want to do the equivalent of the following Python code:
How can I do this?
23 Replies
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.
@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
The read code has an example at the top of the file
Thanks, will try it out!
@lukas can you give an example on how to write files as well? Thanks a lot!
I haven’t written a wrapper for that yet
I haven’t had a need, but will probably get around to it soon
I see. So as far as we know there's currently no way to write to file in Mojo yet?
You can call the CAPIs
i.e write your own fwrite wrapper in the same way I did an fread wrapper
Cool, I can try that out
Thanks!
Here we go" https://github.com/ShuzhaoFeng/mojo-minwa
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
Nice 🙂
@Tenesis thought I'd ping you that I've since added a buffered writer
Awesome. 🥳 Thanks Lukas
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 doingIt is simple with the version 0.4.0 , you have to see the documention of this version 🙏:mojo: :python:
Thanks @Elaid Tebabkha! 🩵
Will check it out now