TGViewer
Data science/ML/AI Data science/ML/AI @datascience_bds · 14K subscribers
Post #1040 1.49K
Data science/ML/AI Hey everyone 👋 Tomorrow we are kicking off a new short & free series called: 📊 Data Importing Series 📊 We’ll go through all the real ways to pull data into Python: → CSV, Excel, JSON and more → Databases & SQL databases → APIs, Google Sheets, even PDFs…
Loading a text file in Python

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
  • ❤ 7
More from @datascience_bds
  1. Oct 13, 20261,000 AI/ML Acronyms & Operational Concepts
  2. Oct 12, 2026Roadmap to Learn SQL
  3. Oct 11, 2026🧮 Why does NumPy let you add a number to an entire matrix? Look at this: import numpy as…
  4. Oct 10, 2026document post
  5. Oct 9, 20268 RAG architectures for AI Engineers, visually explained:
  6. Oct 8, 2026document post
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →