Text files (.txt) are perfect for logs, books, raw notes, or any unstructured data.
With one clean line using pathlib, you can load an entire novel, log file, or dataset into a string
# Loading a text file in Python
filename = 'huck_finn.txt' # Name of the file to open
file = open(filename, mode='r') # Open file in read mode ('r')
# Use encoding='utf-8' if needed
text = file.read() # Read entire content into a string
print(file.closed) # False → file is still open
file.close() # Always close the file when done!
# Prevents memory leaks & file locks
print(file.closed) # Now True → file is safely closed
print(text) # Display the full text content
Next up ➡️ Loading a JSON file in Python
👉Join @datascience_bds for more
Part of the @bigdataspecialist family