Commits

Antoine Pitrou  committed 357bda4

Add exists()

  • Participants
  • Parent commits 8903e1a

Comments (0)

Files changed (2)

     import dummy_threading as threading
 
 from collections import Sequence, defaultdict
-from errno import EINVAL
+from errno import EINVAL, ENOENT
 from functools import wraps
 from itertools import chain, count
 from operator import attrgetter
 
     # Convenience functions for querying the stat results
 
+    def exists(self):
+        """
+        Whether this path exists.
+        """
+        try:
+            self.restat()
+        except OSError as e:
+            if e.errno != ENOENT:
+                raise
+            return False
+        return True
+
     def is_dir(self):
         """
         Whether this path is a directory.

File test_pathlib.py

         p = self.cls('')
         self.assertEqual(p.stat(), os.stat('.'))
 
+    def test_exists(self):
+        P = self.cls
+        p = P(BASE)
+        self.assertIs(True, p.exists())
+        self.assertIs(True, p['dirA'].exists())
+        self.assertIs(True, p['fileA'].exists())
+        self.assertIs(True, p['linkA'].exists())
+        self.assertIs(True, p['linkB'].exists())
+        self.assertIs(False, p['foo'].exists())
+        self.assertIs(False, P('/xyzzy').exists())
+
     def test_open(self):
         p = self.cls(BASE)
         with p['fileA'].open('r') as f: