Skip to content

Commit 5f22741

Browse files
authored
bpo-23706: Add newline parameter to pathlib.Path.write_text (GH-22420) (GH-22420)
* Add _newline_ parameter to `pathlib.Path.write_text()` * Update documentation of `pathlib.Path.write_text()` * Add test case for `pathlib.Path.write_text()` calls with _newline_ parameter passed Automerge-Triggered-By: GH:methane
1 parent 25492a5 commit 5f22741

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

Doc/library/pathlib.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1166,7 +1166,7 @@ call fails (for example because the path doesn't exist).
11661166
.. versionadded:: 3.5
11671167

11681168

1169-
.. method:: Path.write_text(data, encoding=None, errors=None)
1169+
.. method:: Path.write_text(data, encoding=None, errors=None, newline=None)
11701170

11711171
Open the file pointed to in text mode, write *data* to it, and close the
11721172
file::
@@ -1182,6 +1182,9 @@ call fails (for example because the path doesn't exist).
11821182

11831183
.. versionadded:: 3.5
11841184

1185+
.. versionchanged:: 3.10
1186+
The *newline* parameter was added.
1187+
11851188
Correspondence to tools in the :mod:`os` module
11861189
-----------------------------------------------
11871190

Lib/pathlib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,14 +1264,14 @@ def write_bytes(self, data):
12641264
with self.open(mode='wb') as f:
12651265
return f.write(view)
12661266

1267-
def write_text(self, data, encoding=None, errors=None):
1267+
def write_text(self, data, encoding=None, errors=None, newline=None):
12681268
"""
12691269
Open the file in text mode, write to it, and close the file.
12701270
"""
12711271
if not isinstance(data, str):
12721272
raise TypeError('data must be str, not %s' %
12731273
data.__class__.__name__)
1274-
with self.open(mode='w', encoding=encoding, errors=errors) as f:
1274+
with self.open(mode='w', encoding=encoding, errors=errors, newline=newline) as f:
12751275
return f.write(data)
12761276

12771277
def readlink(self):

Lib/test/test_pathlib.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,6 +1510,26 @@ def test_read_write_text(self):
15101510
self.assertRaises(TypeError, (p / 'fileA').write_text, b'somebytes')
15111511
self.assertEqual((p / 'fileA').read_text(encoding='latin-1'), 'äbcdefg')
15121512

1513+
def test_write_text_with_newlines(self):
1514+
p = self.cls(BASE)
1515+
# Check that `\n` character change nothing
1516+
(p / 'fileA').write_text('abcde\r\nfghlk\n\rmnopq', newline='\n')
1517+
self.assertEqual((p / 'fileA').read_bytes(),
1518+
b'abcde\r\nfghlk\n\rmnopq')
1519+
# Check that `\r` character replaces `\n`
1520+
(p / 'fileA').write_text('abcde\r\nfghlk\n\rmnopq', newline='\r')
1521+
self.assertEqual((p / 'fileA').read_bytes(),
1522+
b'abcde\r\rfghlk\r\rmnopq')
1523+
# Check that `\r\n` character replaces `\n`
1524+
(p / 'fileA').write_text('abcde\r\nfghlk\n\rmnopq', newline='\r\n')
1525+
self.assertEqual((p / 'fileA').read_bytes(),
1526+
b'abcde\r\r\nfghlk\r\n\rmnopq')
1527+
# Check that no argument passed will change `\n` to `os.linesep`
1528+
os_linesep_byte = bytes(os.linesep, encoding='ascii')
1529+
(p / 'fileA').write_text('abcde\nfghlk\n\rmnopq')
1530+
self.assertEqual((p / 'fileA').read_bytes(),
1531+
b'abcde' + os_linesep_byte + b'fghlk' + os_linesep_byte + b'\rmnopq')
1532+
15131533
def test_iterdir(self):
15141534
P = self.cls
15151535
p = P(BASE)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added *newline* parameter to ``pathlib.Path.write_text()``.

0 commit comments

Comments
 (0)