newb: Join two string variables

Cameron Walsh cameron.walsh at gmail.com
Tue Dec 5 20:15:31 EST 2006


johnny wrote:
> How do I join two string variables?
> I  want to do:  download_dir + filename.
> download_dir=r'c:/download/'
> filename =r'log.txt'
> 
> I want to get something like this:
> c:/download/log.txt
> 

Hi Johnny,

This is actually two questions:

1.)  How do I concatenate strings
2.)  How do I concatenate pathnames?

Answers:

1.)

>>> download_dir="C:/download/"
>>> filename="log.txt"
>>> path_to_file=download_dir+filename
>>> path_to_file
'C:/download/log.txt'

2.)

>>> import os
>>> filename='log.txt'
>>> path_to_file = os.path.join("C:/download",filename)
>>> path_to_file
'C:/download/log.txt'


Help on function join in module posixpath:

join(a, *p)
    Join two or more pathname components, inserting '/' as needed



Hope it helps,

Cameron.



More information about the Python-list mailing list