diff -r c7233921b612 Doc/library/pathlib.rst
--- a/Doc/library/pathlib.rst Fri Jan 31 15:30:30 2014 -0500
+++ b/Doc/library/pathlib.rst Fri Jan 31 23:43:54 2014 +0200
@@ -794,6 +794,28 @@
if the file's uid isn't found in the system database.
+.. method:: Path.read_bytes()
+
+ Open the file in bytes mode, read it, and close the file.
+
+ >>> p = Path('my_binary_file')
+ >>> p.write_bytes(b'Binary file contents')
+ 20
+ >>> p.read_bytes()
+ b'Binary file contents'
+
+
+.. method:: Path.read_text(encoding=None, errors=None, newline=None)
+
+ Open the file in text mode, read it, and close the file.
+
+ >>> p = Path('my_text_file')
+ >>> p.write_text('Text file contents')
+ 18
+ >>> p.read_text()
+ 'Text file contents'
+
+
.. method:: Path.rename(target)
Rename this file or directory to the given *target*. *target* can be
@@ -887,3 +909,28 @@
Remove this file or symbolic link. If the path points to a directory,
use :func:`Path.rmdir` instead.
+
+
+.. method:: Path.write_bytes(data, append=False, exclusive=False)
+
+ Open the file in bytes mode, write to it, and close the file.
+
+ >>> p = Path('my_binary_file')
+ >>> p.write_bytes(b'Binary file contents')
+ 20
+ >>> p.read_bytes()
+ b'Binary file contents'
+
+
+.. method:: Path.write_text(self, data, encoding=None, errors=None,
+ newline=None, append=False, exclusive=False)
+
+ Open the file in text mode, write to it, and close the file.
+
+ >>> p = Path('my_text_file')
+ >>> p.write_text('Text file contents')
+ 18
+ >>> p.read_text()
+ 'Text file contents'
+
+
diff -r c7233921b612 Lib/pathlib.py
--- a/Lib/pathlib.py Fri Jan 31 15:30:30 2014 -0500
+++ b/Lib/pathlib.py Fri Jan 31 23:43:54 2014 +0200
@@ -1069,6 +1069,45 @@
return io.open(str(self), mode, buffering, encoding, errors, newline,
opener=self._opener)
+ def read_bytes(self):
+ """
+ Open the file in bytes mode, read it, and close the file.
+ """
+ with self.open(mode='rb') as f:
+ return f.read()
+
+ def read_text(self, encoding=None, errors=None, newline=None):
+ """
+ Open the file in text mode, read it, and close the file.
+ """
+ with self.open(mode='r', encoding=encoding,
+ errors=errors, newline=newline) as f:
+ return f.read()
+
+ def write_bytes(self, data, append=False, exclusive=False):
+ """
+ Open the file in bytes mode, write to it, and close the file.
+ """
+ if append and exclusive:
+ raise TypeError('write_bytes does not accept both '
+ '"append" and "exclusive" mode.')
+ mode = 'ab' if append else 'xb' if exclusive else 'wb'
+ with self.open(mode=mode) as f:
+ return f.write(data)
+
+ def write_text(self, data, encoding=None, errors=None,
+ newline=None, append=False, exclusive=False):
+ """
+ Open the file in text mode, write to it, and close the file.
+ """
+ if append and exclusive:
+ raise TypeError('write_text does not accept both '
+ '"append" and "exclusive" mode.')
+ mode = 'a' if append else 'x' if exclusive else 'w'
+ with self.open(mode=mode, encoding=encoding,
+ errors=errors, newline=newline) as f:
+ return f.write(data)
+
def touch(self, mode=0o666, exist_ok=True):
"""
Create this file with the given access mode, if it doesn't exist.
diff -r c7233921b612 Lib/test/test_pathlib.py
--- a/Lib/test/test_pathlib.py Fri Jan 31 15:30:30 2014 -0500
+++ b/Lib/test/test_pathlib.py Fri Jan 31 23:43:54 2014 +0200
@@ -1250,6 +1250,50 @@
self.assertIsInstance(f, io.RawIOBase)
self.assertEqual(f.read().strip(), b"this is file A")
+ def test_read_bytes(self):
+ p = self.cls(BASE)
+ with (p / 'fileA').open('wb') as f:
+ f.write(b'abcdef')
+ f.flush()
+ self.assertEqual((p / 'fileA').read_bytes(), b'abcdef')
+
+ def test_read_text(self):
+ p = self.cls(BASE)
+ with (p / 'fileA').open('w') as f:
+ f.write('abcdef')
+ f.flush()
+ self.assertEqual((p / 'fileA').read_text(), 'abcdef')
+
+ def test_write_bytes(self):
+ p = self.cls(BASE)
+ (p / 'fileA').write_bytes(b'abcdefg')
+ with (p / 'fileA').open('rb') as f:
+ self.assertEqual(f.read(), b'abcdefg')
+ (p / 'fileA').write_bytes(b'hijk', append=True)
+ with (p / 'fileA').open('rb') as f:
+ self.assertEqual(f.read(), b'abcdefghijk')
+ self.assertRaises(TypeError, (p / 'fileA').write_bytes, 'badstr')
+ self.assertRaises(TypeError,
+ (p / 'fileA').write_bytes, b'a',
+ append=True, exclusive=True)
+ self.assertRaises(FileExistsError,
+ (p / 'fileA').write_bytes, b'a', exclusive=True)
+
+ def test_write_text(self):
+ p = self.cls(BASE)
+ (p / 'fileA').write_text('abcdefg')
+ with (p / 'fileA').open('r') as f:
+ self.assertEqual(f.read(), 'abcdefg')
+ (p / 'fileA').write_text('hijk', append=True)
+ with (p / 'fileA').open('r') as f:
+ self.assertEqual(f.read(), 'abcdefghijk')
+ self.assertRaises(TypeError, (p / 'fileA').write_text, b'badbytes')
+ self.assertRaises(TypeError,
+ (p / 'fileA').write_text, 'a',
+ append=True, exclusive=True)
+ self.assertRaises(FileExistsError,
+ (p / 'fileA').write_text, 'a', exclusive=True)
+
def test_iterdir(self):
P = self.cls
p = P(BASE)