summaryrefslogtreecommitdiff
path: root/Lib/genericpath.py
diff options
context:
space:
mode:
authorJack Diederich <jackdied@gmail.com>2006-08-26 18:42:06 +0000
committerJack Diederich <jackdied@gmail.com>2006-08-26 18:42:06 +0000
commitb3383c4424a94e8c565d3c6530a4757e1b2d1954 (patch)
tree80cb479d6d0292319e04a7e94d794698263e9c4c /Lib/genericpath.py
parent2d8d0099959d1fe9f281a48c217060f1a3f8e143 (diff)
downloadcpython-b3383c4424a94e8c565d3c6530a4757e1b2d1954.tar.gz
- Move functions common to all path modules into genericpath.py and have the
OS speicifc path modules import them. - Have os2emxpath import common functions fron ntpath instead of using copies
Diffstat (limited to 'Lib/genericpath.py')
-rw-r--r--Lib/genericpath.py78
1 files changed, 78 insertions, 0 deletions
diff --git a/Lib/genericpath.py b/Lib/genericpath.py
new file mode 100644
index 0000000000..124bdca69c
--- /dev/null
+++ b/Lib/genericpath.py
@@ -0,0 +1,78 @@
+"""
+Path operations common to more than one OS
+Do not use directly. The OS specific modules import the appropriate
+functions from this module themselves.
+"""
+import os
+import stat
+
+__all__ = ['commonprefix', 'exists', 'getatime', 'getctime', 'getmtime',
+ 'getsize', 'isdir', 'isfile']
+
+
+# Does a path exist?
+# This is false for dangling symbolic links on systems that support them.
+def exists(path):
+ """Test whether a path exists. Returns False for broken symbolic links"""
+ try:
+ st = os.stat(path)
+ except os.error:
+ return False
+ return True
+
+
+# This follows symbolic links, so both islink() and isdir() can be true
+# for the same path ono systems that support symlinks
+def isfile(path):
+ """Test whether a path is a regular file"""
+ try:
+ st = os.stat(path)
+ except os.error:
+ return False
+ return stat.S_ISREG(st.st_mode)
+
+
+# Is a path a directory?
+# This follows symbolic links, so both islink() and isdir()
+# can be true for the same path on systems that support symlinks
+def isdir(s):
+ """Return true if the pathname refers to an existing directory."""
+ try:
+ st = os.stat(s)
+ except os.error:
+ return False
+ return stat.S_ISDIR(st.st_mode)
+
+
+def getsize(filename):
+ """Return the size of a file, reported by os.stat()."""
+ return os.stat(filename).st_size
+
+
+def getmtime(filename):
+ """Return the last modification time of a file, reported by os.stat()."""
+ return os.stat(filename).st_mtime
+
+
+def getatime(filename):
+ """Return the last access time of a file, reported by os.stat()."""
+ return os.stat(filename).st_atime
+
+
+def getctime(filename):
+ """Return the metadata change time of a file, reported by os.stat()."""
+ return os.stat(filename).st_ctime
+
+
+# Return the longest prefix of all list elements.
+def commonprefix(m):
+ "Given a list of pathnames, returns the longest common leading component"
+ if not m: return ''
+ s1 = min(m)
+ s2 = max(m)
+ n = min(len(s1), len(s2))
+ for i in xrange(n):
+ if s1[i] != s2[i]:
+ return s1[:i]
+ return s1[:n]
+