Antoine Pitrou avatar Antoine Pitrou committed 02c2082

Add is_file and is_symlink

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()
 
 
 .. 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__ = ()
         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):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.