6

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.

2
  • You can't use "/" ? "C:/Temp/Test" should also do Commented Oct 15, 2012 at 6: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. Commented Oct 15, 2012 at 7:02

1 Answer 1

13

Use os.path.join for this.

os.path.join(r'C:\Temp', 'Test') 
Sign up to request clarification or add additional context in comments.

1 Comment

According to this answer the '\' should be a '/'... I have no windows to double check.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.