The with
statement is excellent for automatically opening and closing files.
with open('myfile','rw') as file:
for line in file:
if not line.isspace():
file.write(line)
More Related Contents:
- Python: How to loop through blocks of lines
- How to count the number of words in a sentence, ignoring numbers, punctuation and whitespace?
- How can I loop through blocks of lines in a file?
- Python: Best Way to remove duplicate character from string
- Expanding English language contractions in Python
- Get raw key value
- Python import csv to list
- Download file from web in Python 3
- How to make separator in pandas read_csv more flexible wrt whitespace, for irregular separators?
- UnboundLocalError with nested function scopes
- Repeating elements of a list n times
- Search for String in all Pandas DataFrame columns and filter
- How to get the nth element of a python list or a default if not available
- How to make two plots side-by-side using Python?
- Py_initialize / Py_Finalize not working twice with numpy
- Pygame: Is there any easy way to find the letter/number of ANY alphanumeric pressed?
- Problem in combining bar plot and line plot (python)
- import pandas_datareader gives ImportError: cannot import name ‘is_list_like’
- Python: CSV write by column rather than row
- Multiprocessing launching too many instances of Python VM
- Read csv from Google Cloud storage to pandas dataframe
- How can I make multiple replacements in a string using a dictionary?
- Read from File, or STDIN
- Get name of current class?
- Check if all values in list are greater than a certain number
- Is it possible to read FTP files without writing them using Python?
- overcome Graphdef cannot be larger than 2GB in tensorflow
- How to use QThread correctly in pyqt with moveToThread()?
- Why am I getting NotImplementedError with async and await on Windows?
- CSRF validation does not work on Django using HTTPS