Snippets

Hiroaki Itoh os.path alternative (force using forward slash rather than backslash, even if on windows)

Updated by Hiroaki Itoh

File fwspath.py Modified

  • Ignore whitespace
  • Hide word diff
         return wrapper
 
     normcase = _deco4str(os.path.normcase)
+    join = _deco4str(os.path.join)
     splitdrive = _deco4tpl(os.path.splitdrive)
     split = _deco4tpl(os.path.split)
     splitext = _deco4tpl(os.path.splitext)
     relpath = _deco4str(os.path.relpath)
 else:
     normcase = os.path.normcase
+    join = os.path.join
     splitdrive = os.path.splitdrive
     split = os.path.split
     splitext = os.path.splitext
 isdir = os.path.isdir
 isfile = os.path.isfile
 ismount = os.path.ismount
-join = os.path.join
 #walk = os.path.walk  # Python 3 doesn't have "os.path.walk"
 sep = os.path.sep
 pathsep = os.path.pathsep
Updated by Hiroaki Itoh

File fwspath.py Modified

  • Ignore whitespace
  • Hide word diff
         return wrapper
 
     normcase = _deco4str(os.path.normcase)
-    join = _deco4str(os.path.join)
     splitdrive = _deco4tpl(os.path.splitdrive)
     split = _deco4tpl(os.path.split)
     splitext = _deco4tpl(os.path.splitext)
     dirname = _deco4str(os.path.dirname)
-    commonprefix = _deco4str(os.path.commonprefix)
     expanduser = _deco4str(os.path.expanduser)
     expandvars = _deco4str(os.path.expandvars)
     normpath = _deco4str(os.path.normpath)
     relpath = _deco4str(os.path.relpath)
 else:
     normcase = os.path.normcase
-    join = os.path.join
     splitdrive = os.path.splitdrive
     split = os.path.split
     splitext = os.path.splitext
     dirname = os.path.dirname
-    commonprefix = os.path.commonprefix
     expanduser = os.path.expanduser
     expandvars = os.path.expandvars
     normpath = os.path.normpath
     realpath = os.path.realpath
     relpath = os.path.relpath
 
+commonprefix = os.path.commonprefix
 curdir = os.path.curdir
 pardir = os.path.pardir
 isabs = os.path.isabs
 isdir = os.path.isdir
 isfile = os.path.isfile
 ismount = os.path.ismount
+join = os.path.join
 #walk = os.path.walk  # Python 3 doesn't have "os.path.walk"
 sep = os.path.sep
 pathsep = os.path.pathsep
Updated by Hiroaki Itoh

File fwspath.py Modified

  • Ignore whitespace
  • Hide word diff
 __all__ = ["normcase","isabs","join","splitdrive","split","splitext",
            "basename","dirname","commonprefix","getsize","getmtime",
            "getatime","getctime", "islink","exists","lexists","isdir","isfile",
-           "ismount","walk","expanduser","expandvars","normpath","abspath",
+           "ismount","expanduser","expandvars","normpath","abspath",
            "splitunc","curdir","pardir","sep","pathsep","defpath","altsep",
            "extsep","devnull","realpath","supports_unicode_filenames","relpath"]
 
 isdir = os.path.isdir
 isfile = os.path.isfile
 ismount = os.path.ismount
-walk = os.path.walk
+#walk = os.path.walk  # Python 3 doesn't have "os.path.walk"
 sep = os.path.sep
 pathsep = os.path.pathsep
 defpath = os.path.defpath
 extsep = os.path.extsep
 devnull = os.path.devnull
 supports_unicode_filenames = os.path.supports_unicode_filenames
-
-
-#def normdir(path):
-#    path = normpath(path)
-#    if not path:
-#        return "./"
-#    if not path.endswith("/"):
-#        path += "/"
-#    return path
Updated by Hiroaki Itoh

File fwspath.py Modified

  • Ignore whitespace
  • Hide word diff
 supports_unicode_filenames = os.path.supports_unicode_filenames
 
 
-def normdir(path):
-    path = normpath(path)
-    if not path:
-        return "./"
-    if not path.endswith("/"):
-        path += "/"
-    return path
+#def normdir(path):
+#    path = normpath(path)
+#    if not path:
+#        return "./"
+#    if not path.endswith("/"):
+#        path += "/"
+#    return path
Updated by Hiroaki Itoh

File fwspath.py Modified

  • Ignore whitespace
  • Hide word diff
 
 import os
 from functools import wraps
+import sys
 
 __all__ = ["normcase","isabs","join","splitdrive","split","splitext",
            "basename","dirname","commonprefix","getsize","getmtime",
 #
 #
 #
-def _decorator1(func):
-    @wraps(func)
-    def wrapper(*args, **kwargs):
-        return func(*args, **kwargs).replace(os.sep, "/")
-    return wrapper
+if sys.platform == 'win32':
+    def _deco4str(func):
+        @wraps(func)
+        def wrapper(*args, **kwargs):
+            return func(*args, **kwargs).replace(os.sep, "/")
+        return wrapper
 
-def _decorator2(func):
-    @wraps(func)
-    def wrapper(*args, **kwargs):
-        return tuple((s.replace(os.sep, "/")
-                 for s in func(*args, **kwargs)))
-    return wrapper
+    def _deco4tpl(func):
+        @wraps(func)
+        def wrapper(*args, **kwargs):
+            return tuple((s.replace(os.sep, "/")
+                     for s in func(*args, **kwargs)))
+        return wrapper
 
-normcase = _decorator1(os.path.normcase)
-join = _decorator1(os.path.join)
-splitdrive = _decorator2(os.path.splitdrive)
-split = _decorator2(os.path.split)
-splitext = _decorator2(os.path.splitext)
-dirname = _decorator1(os.path.dirname)
-commonprefix = _decorator1(os.path.commonprefix)
-expanduser = _decorator1(os.path.expanduser)
-expandvars = _decorator1(os.path.expandvars)
-normpath = _decorator1(os.path.normpath)
-abspath = _decorator1(os.path.abspath)
-splitunc = _decorator2(os.path.splitunc)
-realpath = _decorator1(os.path.realpath)
-relpath = _decorator1(os.path.relpath)
+    normcase = _deco4str(os.path.normcase)
+    join = _deco4str(os.path.join)
+    splitdrive = _deco4tpl(os.path.splitdrive)
+    split = _deco4tpl(os.path.split)
+    splitext = _deco4tpl(os.path.splitext)
+    dirname = _deco4str(os.path.dirname)
+    commonprefix = _deco4str(os.path.commonprefix)
+    expanduser = _deco4str(os.path.expanduser)
+    expandvars = _deco4str(os.path.expandvars)
+    normpath = _deco4str(os.path.normpath)
+    abspath = _deco4str(os.path.abspath)
+    splitunc = _deco4tpl(os.path.splitunc)
+    realpath = _deco4str(os.path.realpath)
+    relpath = _deco4str(os.path.relpath)
+else:
+    normcase = os.path.normcase
+    join = os.path.join
+    splitdrive = os.path.splitdrive
+    split = os.path.split
+    splitext = os.path.splitext
+    dirname = os.path.dirname
+    commonprefix = os.path.commonprefix
+    expanduser = os.path.expanduser
+    expandvars = os.path.expandvars
+    normpath = os.path.normpath
+    abspath = os.path.abspath
+    splitunc = os.path.splitunc
+    realpath = os.path.realpath
+    relpath = os.path.relpath
 
 curdir = os.path.curdir
 pardir = os.path.pardir
  1. Prev
  2. 1
  3. 2
  4. Next