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.
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:
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.
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.
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.
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.
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.pyor%runwhen the file is Python code you want to execute - use
open,csv, orjsonwhen 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
execwhen 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.pywhen 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
csvandjsonfor data files. - Prefer IPython
%runwhen you are working in an IPython-style console.
Related reading
- How do I log a Python error with debug information?
- How do I look inside a Python object?
- How do I loop through a list by twos?
- How do I lowercase a string in Python?
- How do I make a flat list out of a list of lists?
- How do I make function decorators and chain them together?
- How do I measure elapsed time in Python?
- How do I merge a list of dicts into a single dict?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.