Read a text file into one big string

Change Myname to your name. The Microsoft Windows filename is in a raw string because it contains backslashes.

I sent the error messages to the standard error output because this Python program is not interactive, i.e., it doesn’t ask the user questions with the input function.

Inputting an entire file all at once is called slurping the file.

onebigstring.py

The output is a copy of one of the README files that you installed when you installed Python:

This directory contains a collection of demonstration scripts for
various aspects of Python programming.

beer.py        Well-known programming example: Bottles of beer.
eiffel.py      Python advanced magic: A metaclass for Eiffel post/preconditions.
hanoi.py       Well-known programming example: Towers of Hanoi.
life.py        Curses programming: Simple game-of-life.
markov.py      Algorithms: Markov chain simulation.
mcast.py       Network programming: Send and receive UDP multicast packets.
queens.py      Well-known programming example: N-Queens problem.
redemo.py      Regular Expressions: GUI script to test regexes.
rpython.py     Network programming: Small client for remote code execution.
rpythond.py    Network programming: Small server for remote code execution.
sortvisu.py    GUI programming: Visualization of different sort algorithms.
ss1.py         GUI/Application programming: A simple spreadsheet application.
vector.py      Python basics: A vector class with demonstrating special methods.

To run one of these demo programs (e.g., beer.py), open the macOS Terminal application and say

cd /Library/Frameworks/Python.framework/Versions/3.7/share/doc/python3.7/examples/Tools/demo
pwd
ls -l
./beer.py

Things to try

  1. Change the filename from
    #macOS
    filename = "/Library/Frameworks/Python.framework/Versions/3.7/share/doc/python3.7/examples/Tools/demo/README"
    
    #Microsoft Windows
    #filename = r"C:\Users\Myname\AppData\Local\Programs\Python\Python37-32\Tools\demo\README"
    
    to
    import getpass   #get password
    
    username = getpass.getuser()
    
    #macOS
    filename = "/Library/Frameworks/Python.framework/Versions/3.7/share/doc/python3.7/examples/Tools/demo/README"
    
    #Microsoft Windows
    #filename = r"C:\Users\{username}\AppData\Local\Programs\Python\Python37-32\Tools\demo\README"
    
  2. Better yet, create the filename like this:
    if sys.platform.startswith("darwin"):    #macOS Sierra 10.15
        filename = "/Library/Frameworks/Python.framework/Versions/3.7/share/doc/python3.7/examples/Tools/demo/README"
    elif sys.platform.startswith("win32"):   #Microsoft Windows 7 Home Premium
        filename = r"C:\Users\{username}\AppData\Local\Programs\Python\Python37-32\Tools\demo\README"
    else:
        print("Unknown platform", sys.platform)
        sys.exit(1)
    
  3. Even better, don’t write lines of code that are longer than 79 characters. Make sure the backslash is the last character on the line with nothing after it, not even a space. See Explicit line joining and String literal concatenation.
    if sys.platform.startswith("darwin"):    #macOS Sierra 10.15
        filename = "/Library/Frameworks/Python.framework" \
            "/Versions/3.7/share/doc/python3.7/examples/Tools/demo/README"
    
    elif sys.platform.startswith("win32"):   #Microsoft Windows 7 Home Premium
        filename = r"C:\Users\{username}\AppData\Local" \
            "\Programs\Python\Python37-32\Tools\demo\README"
    
    else:
        print("Unknown platform", sys.platform)
        sys.exit(1)
    

    On my Fedora Linux machine, sys.platform is "linux".

  4. Insert the following after creating oneBigString.
    count = oneBigString.count("\n")   #How many newline characters does the string contain?
    print(f"The file contains {count} lines.")
    
    The file contains 16 lines.
    
    How many characters does the file contain?
  5. Split the big string into separate lines and print them one by one. splitlines gives you a list of strings. With no argument, splitlines removes the newline from the end of each string.
    for line in oneBigString.splitlines():
        print(line)
    
    for i, line in enumerate(oneBigString.splitlines(), start = 1):
        print(i, line)
    
    1 This directory contains a collection of demonstration scripts for
    2 various aspects of Python programming.
    3
    4 beer.py        Well-known programming example: Bottles of beer.
    5 eiffel.py      Python advanced magic: A metaclass for Eiffel post/preconditions.
    6 hanoi.py       Well-known programming example: Towers of Hanoi.
    7 life.py        Curses programming: Simple game-of-life.
    8 markov.py      Algorithms: Markov chain simulation.
    9 mcast.py       Network programming: Send and receive UDP multicast packets.
    10 queens.py      Well-known programming example: N-Queens problem.
    11 redemo.py      Regular Expressions: GUI script to test regexes.
    12 rpython.py     Network programming: Small client for remote code execution.
    13 rpythond.py    Network programming: Small server for remote code execution.
    14 sortvisu.py    GUI programming: Visualization of different sort algorithms.
    15 ss1.py         GUI/Application programming: A simple spreadsheet application.
    16 vector.py      Python basics: A vector class with demonstrating special methods.
    

    Since the number of lines in this file is a two-digit number (16), print each line number using two characters:

    lines = oneBigString.splitlines()         #lines is a list of strings
    numberOfLines = len(lines)
    numberOfDigits = len(str(numberOfLines))
    
    for i, line in enumerate(lines, start = 1):
        print(f"{i:{numberOfDigits}} {line}") #nested curly braces: a pocket within a pocket
    
     1 This directory contains a collection of demonstration scripts for
     2 various aspects of Python programming.
     3
     4 beer.py        Well-known programming example: Bottles of beer.
     5 eiffel.py      Python advanced magic: A metaclass for Eiffel post/preconditions.
     6 hanoi.py       Well-known programming example: Towers of Hanoi.
     7 life.py        Curses programming: Simple game-of-life.
     8 markov.py      Algorithms: Markov chain simulation.
     9 mcast.py       Network programming: Send and receive UDP multicast packets.
    10 queens.py      Well-known programming example: N-Queens problem.
    11 redemo.py      Regular Expressions: GUI script to test regexes.
    12 rpython.py     Network programming: Small client for remote code execution.
    13 rpythond.py    Network programming: Small server for remote code execution.
    14 sortvisu.py    GUI programming: Visualization of different sort algorithms.
    15 ss1.py         GUI/Application programming: A simple spreadsheet application.
    16 vector.py      Python basics: A vector class with demonstrating special methods.
    
  6. If you would prefer to get a list of strings without having the newlines removed, change read to readlines:
    lines = fileObject.readlines()   #Read the entire file.  lines is a list of strings.
    fileObject.close()
    
    for line in lines:
        print(line, end = "")   #line already has a newline.
    
  7. The variable fileObject is holding a file-like object. Exactly what type of value is this? Here is the type, the base class of the type, the base class of the base class of the type, etc., all the way back down to class object.
    print(f"type(fileObject) = {type(fileObject)}")
    print()
    
    for t in type(fileObject).mro(): #method resolution order
        print(t)                     #t is a type
    
    print()
    
    type(fileObject) = <class '_io.TextIOWrapper'>
    
    <class '_io.TextIOWrapper'>
    <class '_io._TextIOBase'>
    <class '_io._IOBase'>
    <class 'object'>
    
    The read function called by the expression fileObject.read() is io.TextIOBase.read because class io.TextIOBase is the highest class that has a read function in in the above list of classes. (Class io.TextIOWrapper does not have a read function.) Similarly, the close is io.IOBase.close. (Classes io.TextIOBase and io.TextIOWrapper do not have a close function.)