Commits

Antoine Pitrou  committed 02c2082

Add is_file and is_symlink

  • Participants
  • Parent commits 100f779
  • Tags 0.6

Comments (0)

Files changed (4)

 Version 0.6
 ^^^^^^^^^^^
 
+- Add Path.is_file() and Path.is_symlink()
 - Add Path.glob() and Path.rglob()
 - Add PurePath.match()
 

File docs/index.rst

 
 .. method:: Path.is_dir()
 
-   Return True if the path points to a directory, False if it points to
-   another kind of file::
+   Return True if the path points to a directory (or a symbolic link
+   pointing to a directory), False if it points to another kind of file.
 
-      >>> Path('.').is_dir()
-      True
-      >>> Path('setup.py').is_dir()
-      False
+
+.. method:: Path.is_file()
+
+   Return True if the path points to a regular file (or a symbolic link
+   pointing to a regular file), False if it points to another kind of file.
+
+
+.. method:: Path.is_symlink()
+
+   Return True if the path points to a symbolic link, False otherwise.
 
 
 .. method:: Path.lchmod(mode)
 from functools import wraps
 from itertools import chain, count
 from operator import attrgetter
-from stat import S_ISDIR
+from stat import S_ISDIR, S_ISLNK, S_ISREG
 
 
 supports_symlinks = True
         """
         return S_ISDIR(self._stat.st_mode)
 
+    def is_file(self):
+        """
+        Whether this path is a regular file (also True for symlinks pointing
+        to regular files).
+        """
+        return S_ISREG(self._stat.st_mode)
+
+    def is_symlink(self):
+        """
+        Whether this path is a symbolic link.
+        """
+        st = self.lstat()
+        return S_ISLNK(st.st_mode)
+
 
 class PosixPath(Path, PurePosixPath):
     __slots__ = ()

File test_pathlib.py

         P = self.cls(BASE)
         self.assertTrue(P['dirA'].is_dir())
         self.assertFalse(P['fileA'].is_dir())
+        if not symlink_skip_reason:
+            self.assertFalse(P['linkA'].is_dir())
+            self.assertTrue(P['linkB'].is_dir())
+
+    def test_is_file(self):
+        P = self.cls(BASE)
+        self.assertTrue(P['fileA'].is_file())
+        self.assertFalse(P['dirA'].is_file())
+        if not symlink_skip_reason:
+            self.assertTrue(P['linkA'].is_file())
+            self.assertFalse(P['linkB'].is_file())
+
+    def test_is_symlink(self):
+        P = self.cls(BASE)
+        self.assertFalse(P['fileA'].is_symlink())
+        self.assertFalse(P['dirA'].is_symlink())
+        if not symlink_skip_reason:
+            self.assertTrue(P['linkA'].is_symlink())
+            self.assertTrue(P['linkB'].is_symlink())
 
 
 class PathTest(_BasePathTest):