Python
File I/O
Python Console
File Loading
Programming Tutorial

How do I load a file into the python console?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

“Load a file into the Python console” can mean two different things. Sometimes you want to execute a Python script inside an interactive session. Other times you want to read a data file, such as text, CSV, or JSON, so you can inspect it interactively. The right answer depends on which of those you actually mean.

Execute a Python File in an Interactive Session

If the file is a Python script and you want its variables available after execution, a common command-line pattern is:

bash
python -i script.py

The script runs first, and then Python drops you into an interactive console with the script’s globals loaded.

This is often the cleanest way to inspect program state without manually copying code into the REPL.

Execute a Script From Inside the Console

If you are already inside the interpreter, you can execute a file manually.

python
1with open("script.py", "r", encoding="utf-8") as f:
2    code = f.read()
3
4exec(code, globals())

That runs the file in the current interactive namespace. It works, but it should be used carefully because exec executes arbitrary Python code directly.

For trusted local scripts, it is fine. For untrusted files, it is unsafe.

Read a Text File Into the Console

If you mean “load file contents so I can inspect them,” then just open and read the file.

python
1with open("notes.txt", "r", encoding="utf-8") as f:
2    text = f.read()
3
4print(text)

This loads the entire text file into a Python string. For very large files, prefer iterating line by line instead of reading everything at once.

Load CSV or JSON Interactively

Structured data usually deserves a parser rather than a plain text read.

python
1import csv
2
3with open("data.csv", newline="", encoding="utf-8") as f:
4    rows = list(csv.reader(f))
5
6print(rows[:3])
python
1import json
2
3with open("config.json", "r", encoding="utf-8") as f:
4    data = json.load(f)
5
6print(data)

In the console, these parsed objects are often more useful than raw text because you can inspect and manipulate them immediately.

IPython and Jupyter Have Better Tools

If you are using IPython, %run is often a better script-loading command than manual exec.

python
%run script.py

That keeps the workflow cleaner and gives you better interactive tooling. So if “Python console” really means IPython or Jupyter, use the environment’s features instead of forcing plain-interpreter patterns.

Choose Based on Intent

A useful rule is:

  • use python -i script.py or %run when the file is Python code you want to execute
  • use open, csv, or json when the file is data you want to inspect

Those are different tasks, even though people often describe both as “loading a file into the console.”

Common Pitfalls

  • Using exec when you really only needed to read a data file.
  • Reading a large file fully into memory when line-by-line processing would be safer.
  • Treating JSON or CSV as plain text instead of parsing it into Python objects.
  • Executing untrusted Python files inside an interactive session.

Summary

  • First decide whether the file is code or data.
  • Use python -i script.py when you want an interactive session after running a script.
  • Use exec(..., globals()) only when you intentionally want to execute code inside the current console.
  • Use normal file I/O or parsers such as csv and json for data files.
  • Prefer IPython %run when you are working in an IPython-style console.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.