The simplest:
with open('topology_list.txt') as topo_file:
for line in topo_file:
print line, # The comma to suppress the extra new line char
Yes, you can iterate through the file handle, no need to call readlines()
. This way, on large files, you don’t have to read all the lines (that’s what readlines()
does) at once.
Note that the line
variable will contain the trailing new line character, e.g. “this is a line\n”
Related Contents:
- How do I connect to a MySQL Database in Python?
- Python rounding error with float numbers [duplicate]
- How to disable Python warnings?
- Add a prefix to all Flask routes
- Can not click on a Element: ElementClickInterceptedException in Splinter / Selenium
- How do I run a Python program in the Command Prompt in Windows 7?
- Disable Tensorflow debugging information
- python pandas: apply a function with arguments to a series
- How to make a Tkinter window jump to the front?
- python class instance variables and class variables
- String concatenation without ‘+’ operator
- Does Python support MySQL prepared statements?
- What’s the best way to parse command line arguments? [closed]
- How can one continuously generate and track several random objects with a time delay in pygame? [duplicate]
- How to extract parameters from a list and pass them to a function call [duplicate]
- Restart python-script from within itself
- Python’s json module, converts int dictionary keys to strings
- Operation on every pair of element in a list
- Generating sublists using multiplication ( * ) unexpected behavior [duplicate]
- Why does map return a map object instead of a list in Python 3?
- str.strip() strange behavior
- Return list of items in list greater than some value
- How to use asyncio with existing blocking library?
- Composing functions in python
- Finding the Values of the Arrow Keys in Python: Why are they triples?
- Create file but if name exists add number
- Regular expression to extract URL from an HTML link
- How to select Python version in PyCharm?
- Defining “boolness” of a class in python
- Adding a particle effect to my clicker game
- lambda function don’t closure the parameter in Python? [duplicate]
- Count occurrences of each of certain words in pandas dataframe
- Python program to check matching of simple parentheses
- Keep a subprocess alive and keep giving it commands? Python
- Error loading MySQLdb Module ‘Did you install mysqlclient or MySQL-python?’
- How to redirect from one domain to another and set cookies or headers for the other domain?
- What encoding does open() use by default?
- Adding a matplotlib legend
- Calling functions by array index in Python
- Python “Every Other Element” Idiom [duplicate]
- Natural/Relative days in Python
- python is not recognized as an internal or external command [duplicate]
- Matplotlib: how to set the current figure?
- Cancellable threading.Timer in Python
- Python – Printing a dictionary as a horizontal table with headers
- Change console font in Windows
- Input and output numpy arrays to h5py
- Can you define aliases for imported modules in Python?
- Running maximum of numpy array values
- Adding a column in pandas df using a function