Enrico Tuvera Jr
Enrico Tuvera Jr

Reputation: 2817

Reading a file via open().read() vs storing it in a variable

I've written this small app in Python that will generate paragraphs of dummy text, kind of like this site, except it'll work offline. Right now you're supposed to provide a reasonably long text file (I'm currently using books from Project Gutenberg), which it will call open() and then read() on to get the initial string for the operation, but what's to stop me from just including the whole text file in the program, as a variable? i.e

lorem_ipsum = """
***full text of De finibus bonorum et malorum***
***no seriously***
***yeah...***
"""

Are there any disadvantages to doing this versus reading it in from a separate text file?

Upvotes: 3

Views: 463

Answers (1)

Daniel Quinn
Daniel Quinn

Reputation: 6388

There's a few problems:

  • Your program becomes really hard to read.
  • You run the risk of """ appearing in the text and blowing everything up
  • You break the whole concept of content existing in the format in which it belongs. The book in question is a text file, it should live in a text file and if Python needs it, it should pull it from there. Otherwise newcomers to the app have to look everywhere for every type of data. not fun.

Upvotes: 12

Related Questions