Function adas.writefile

def writefile(file=None, content=None, comments=None, trim=True, append=False)

Write (or append) a list of strings to a named file.

Parameters

file : str
name of file to be opened.
content : list
contents of the file as a list of strings.
comments : list
comments for the end of the file as a list of strings.
trim : bool
if True remove whitespace at end of line. Default is True.
append : bool
add content to end of an existing file. Default is False.

Returns

lines : list
file contents as a list of strings
num : int
number of lines in file.

Notes

Convenience single line function.

Version History

  • Martin O'Mullane, 13-11-2019
    • First version

Example

>>> import adas as adas
>>> import os
>>> lines = ['helium             -2',
...          ' 0  2.45873879d+01  1s2 ( 1)0(  0.0)',
...          ' 1  5.44177631d+01  1s1 ( 2)0(  0.5)']
>>> my_file='/tmp/test.txt'
>>> os.path.exists(my_file)
False
>>> adas.writefile(file=my_file, content=lines, comments=['I did this'])
>>> os.path.exists(my_file)
True
>>> lines[0:3]