How can I add a trailing slash (/
for *nix, \
for win32) to a directory string, if the tailing slash is not already there? Thanks!
views:
201answers:
4
+1
A:
You can do it manually by:
path = ...
import os
if not path.endswith(os.path.sep):
path += os.path.sep
However, it is usually much cleaner to use os.path.join
.
Max Shawabkeh
2010-04-29 09:33:44
A:
You could use something like this:
os.path.normcase(path)
Normalize the case of a pathname. On Unix and Mac OS X, this returns the path unchanged; on case-insensitive filesystems, it converts the path to lowercase. On Windows, it also converts forward slashes to backward slashes.
Else you could look for something else on this page
Bloeper
2010-04-29 09:35:08
+6
A:
Since you want to connect a directory and a filename, use
os.path.join(directory, filename)
If you want to get rid of .\..\..\blah\
paths, use
os.path.join(os.path.normpath(directory), filename)
Tim Pietzcker
2010-04-29 09:54:50