[Tutor] Simple question: Creating a string with a directory structure

Terry Carroll carroll at tjc.com
Tue Jan 27 22:51:13 EST 2004


On Tue, 27 Jan 2004, Mike Yuen wrote:

> What i'm trying to do concatenate a bunch of strings to make a file path.  
> For example,
> 
> A = "c:/WINNT"
> B = "/"
> C = "Program Files"
> 
> When I go:
> D = A+B+C
> print D # hoping to get c:/winnt/program files.
> 
> INstead, when I output D, I get <type 'str'>
> How do I fix this?

Can you do a copy & paste and show us exactly what you're putting in and 
what you're getting?

That works alright for me:

>>> A = "c:/WINNT"
>>> B = "/"
>>> C = "Program Files"
>>> D=A+B+C
>>> D
'c:/WINNT/Program Files'

You might however, look into using os.path.join, which is made for this 
kind of thing and takes care of escapes and stuff:

>>> X = "C:"
>>> Y = "\\WINNT"
>>> Z = "Program Files"
>>> os.path.join(X,Y,Z)
'C:\\WINNT\\Program Files'





More information about the Tutor mailing list