Function adas.readfile
def readfile(file=None, lower=False, upper=False, no_blank_lines=False, comments=None)
-
Reads a text file into a list of strings and returns this and the number of lines in the file.
Parameters
file
:str
- name of file to be opened.
lower
:bool
- if True convert the test to lowercase, default is False
upper
:bool
- if True convert the test to uppercase, default is False
no_blank_lines
:bool
- if True don't return blank lines, default is False
comments
:str
orsequence
ofstr
orNone
, optional- characters or list of characters used to indicate the start of a comment. None implies that comment lines are returned and this is the default.
Returns
lines
:list
- file contents as a list of strings
num
:int
- number of lines in file.
Notes
Convenience routine to make opening the file, reading it, splitting into a list and counting the number of lines into a single line function.
Version History
-
Martin O'Mullane, 23-11-2019
- First version
-
Martin O'Mullane, 16-02-2021
- Add options to return the contents of the file in all upper or lower case.
-
Martin O'Mullane, 08-09-2021
- Add no_blank_lines and comments filtering options.
Example
>>> import adas as adas >>> lines, num_lines = adas.readfile('/home/adas/adas/adf00/he.dat') >>> num_lines 14 >>> lines[0:3] ['helium -2', ' 0 2.45873879d+01 1s2 ( 1)0( 0.0)', ' 1 5.44177631d+01 1s1 ( 2)0( 0.5)']