newb: Join two string variables

johnny rampeters at gmail.com
Tue Dec 5 19:56:12 EST 2006


In my code, I have the following:

p = posixpath.basename(e).strip
filename = download_dir+p

I am getting the following error:

 filename = download_dir+p
TypeError: cannot concatenate 'str' and 'builtin_function_or_method'
objects


Cameron Walsh wrote:
> 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