Use a for
loop on a file object to read it line-by-line. Use with open(...)
to let a context manager ensure that the file is closed after reading:
with open("log.txt") as infile:
for line in infile:
print(line)
More Related Contents:
- python program to divide numbers in file in selected order [closed]
- How do I delete a file or folder in Python?
- UnicodeDecodeError: ‘charmap’ codec can’t decode byte X in position Y: character maps to
- How do I concatenate text files in Python?
- Get last n lines of a file, similar to tail
- How can I open multiple files using “with open” in Python?
- Read .mat files in Python
- Merge PDF files
- Reading from a frequently updated file
- open() in Python does not create a file if it doesn’t exist
- Does reading an entire file leave the file handle open?
- How to make file creation an atomic operation?
- Python read JSON file and modify
- Read from a log file as it’s being written using python
- Python, Pandas : write content of DataFrame into text File
- How to delete specific strings from a file?
- Replace string within file contents [duplicate]
- Batch Renaming of Files in a Directory
- How to solve “OSError: telling position disabled by next() call”
- An integer is required? open()
- Python Random Access File
- How to read tokens without reading whole line or file
- Parsing apache log files
- Open files in ‘rt’ and ‘wt’ modes
- Trying to use open(filename, ‘w’ ) gives IOError: [Errno 2] No such file or directory if directory doesn’t exist
- Python file operations
- Speed up writing to files
- How to write to a file without overwriting current contents? [duplicate]
- python write string directly to tarfile
- Change file to read-only mode in Python