Skip to content

Commit 9fe8b41

Browse files
add keep_end in read_lines
1 parent 403e5aa commit 9fe8b41

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

pysenal/io/file.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
_LINE_BREAK_TUPLE = tuple(_LINE_BREAKS)
1515

1616

17-
def read_lines(filename, encoding=_ENCODING_UTF8, strip=False, skip_empty=False):
17+
def read_lines(filename, encoding=_ENCODING_UTF8, keep_end=False, strip=False, skip_empty=False):
1818
"""
1919
read lines in text file
2020
:param filename: file path
2121
:param encoding: encoding of the file, default is utf-8
22+
:param keep_end: whether keep line break in result lines
2223
:param strip: whether strip every line, default is False
2324
:param skip_empty: whether skip empty line, when strip is False, judge after strip
2425
:return: lines
@@ -33,12 +34,16 @@ def read_lines(filename, encoding=_ENCODING_UTF8, strip=False, skip_empty=False)
3334
if skip_empty:
3435
lines = []
3536
for line in f.read().splitlines(True):
36-
line = line.strip(_LINE_BREAKS)
37+
if not keep_end:
38+
line = line.rstrip(_LINE_BREAKS)
3739
if line:
3840
lines.append(line)
3941
return lines
4042
else:
41-
return [l.strip(_LINE_BREAKS) for l in f.read().splitlines(True)]
43+
if not keep_end:
44+
return [l.rstrip(_LINE_BREAKS) for l in f.read().splitlines(True)]
45+
else:
46+
return f.read().splitlines(True)
4247

4348

4449
def read_lines_lazy(src_filename, encoding=_ENCODING_UTF8):

0 commit comments

Comments
 (0)