5

This should be easy enough, but I'm stuck on it for too much.

How do I concatenate two folder names?

For example:

ws = r'C:\Temp' 
folder = "Test"

I want to get a result of c:\Temp\Test but everything I do results in c:\TempTest.

I can't use + "\" because it's invalid operation.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
user1746222
  • 51
  • 1
  • 1
  • 2
  • You can't use "/" ? "C:/Temp/Test" should also do – Anshu Oct 15 '12 at 06:56
  • 2
    You should definitely use @Tichodroma's answer. This will handle path concatenation correctly and platform-independently for you in all circumstances. The reason why `"\"` (or even `r"\"`) didn't work is that the backslash is an escape character. Even in a raw string, the backslash is an escape character for the single use case of escaping the quote character. Therefore, you can't have a string that ends in a single backslash. – Tim Pietzcker Oct 15 '12 at 07:02

1 Answers1

12

Use os.path.join for this.

os.path.join(r'C:\Temp', 'Test')
  • 2
    According to [this answer](http://stackoverflow.com/a/2422864/383793) the '\' should be a '/'... I have no windows to double check. – Chris Wesseling Oct 15 '12 at 07:09