Reputation: 1512
I have been conscientiously using os.path.sep instead of a literal '/'. Thus I was surprised to look at the source for os.path.join today and see it using the literal.
I gather that on Windows platforms some lower level library routines convert '/' to '\' when calling the file system or other '/' using OS functions.
Is it safe just to use the literal without jeopardizing portability to Windows?
def join(a, *p):
path = a
for b in p:
if b.startswith('/'):
path = b
elif path == '' or path.endswith('/'):
path += b
else:
path += '/' + b
return path
Upvotes: 1
Views: 353
Reputation: 550
Python has different sources for different OS's. You can look at files macpath.py, ntpath.py and posixpath.py implementing join function for corresponding platforms. Each of the files defines sep variable to indicate separation symbol for corresponding platform. I would say it is safe to further use os.path.sep otherwise the code will become incompatible with other OS's.
Upvotes: 1