Verwendung os.path.join()
:
import os
fullpath = os.path.join(base_dir, filename)
Das os.path Modul enthält alle Methoden Sie für plattformunabhängige Weg Manipulation benötigen sollten, aber im Falle müssen Sie wissen, was der Pfadseparator auf der aktuellen Plattform ist, können Sie Verwenden Sie os.sep
.
import os
class u(str):
"""
Class to deal with urls concat.
"""
def __init__(self, url):
self.url = str(url)
def __add__(self, other):
if isinstance(other, u):
return u(os.path.join(self.url, other.url))
else:
return u(os.path.join(self.url, other))
def __unicode__(self):
return self.url
def __repr__(self):
return self.url
Die Nutzung ist:
a = u("http://some/path")
b = a + "and/some/another/path" # http://some/path/and/some/another/path
mögliches Duplikat von [Plattformunabhängige Dateipfade?] (Http://stackoverflow.com/questions/6036129/platform-independent-file-paths) –