150

How can I add a trailing slash (/ for *nix, \ for win32) to a directory string, if the tailing slash is not already there? Thanks!

10
  • 9
    You should use the os.path module (docs.python.org/library/os.path.html) instead of manipulating strings directly. Use os.path.join to concatenate path components.
    – kennytm
    Commented Apr 29, 2010 at 9:33
  • 2
    thanks Tim for reminding, after reading the doc, I think the answer to my real question is os.path.join(folder, filename) and I don't have to worry about the slash at all. man, I am going to love python ;-)
    – ohho
    Commented Apr 29, 2010 at 9:51
  • 2
    That's why I asked - don't do this yourself, use os.path.join and let the standard library figure out the correct path separator. Commented Apr 29, 2010 at 9:52
  • 2
    @S.Lott I won't consider this 'bad'. the question is a valid question by itself. it's only a part of a bigger (one may say real) question.
    – ohho
    Commented Apr 29, 2010 at 10:06
  • 8
    Depending on what you're doing, however, the question may still be of interest. For example, if you're using Python to supply input to another program, you might have a case where an argument needs a slash-ended path. That may be out of your control; and is a perfectly valid use case for Marco's answer below. Commented Dec 10, 2012 at 19:57

4 Answers 4

247

os.path.join(path, '') will add the trailing slash if it's not already there.

You can do os.path.join(path, '', '') or os.path.join(path_with_a_trailing_slash, '') and you will still only get one trailing slash.

2
  • 4
    Also adds a slash when path happens to be a file rather than a directory. That's slightly inconvenient.
    – Martin
    Commented Aug 18, 2019 at 20:28
  • 1
    There seems to be an exception: print(os.path.join(r'\\server\c','')) gives \\server\c (whereas print(os.path.join(r'\\server\c\x','')) gives \\server\c\x\ )
    – omasoud
    Commented Dec 14, 2020 at 7:00
35

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)
19

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.

1
  • 1
    Actually, checking os.path.sep is not the best approach here, since on windows the path can end both in / as well as \ .
    – Danon
    Commented Feb 16, 2023 at 20:53
-2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.