What's a reasonable way to read an entire text file as a single string?

后端 未结 2 1205
[愿得一人]
[愿得一人] 2021-02-02 05:15

I am sure this is an easy one; I just couldn\'t find the answer immediately from Google.

I know I could do this (right?):

text = \"\"
File.open(         


        
相关标签:
2条回答
  • 2021-02-02 05:57

    IO.read() is what you're looking for.
    File is a subclass of IO, so you may as well just use:

    text = File.read(path)
    

    Can't get more intuitive than that.

    0 讨论(0)
  • 2021-02-02 06:11

    What about IO.read()?

    Edit: IO.read(), as an added bonus, closes the file for you.

    0 讨论(0)
提交回复
热议问题