By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,652 Members | 1,756 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,652 IT Pros & Developers. It's quick & easy.

newb: Join two string variables

P: n/a
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

Dec 5 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a

johnny wrote:
How do I join two string variables?
I want to do: download_dir + filename.
That should do it. :-)

You can concatenate strings using the plus operator. For large number
of strings it is very inefficient. (Particularly in versions of Python
pre 2.4 or in IronPython.)

For these situations it is better to collect the strings in a list and
then join them using :

''.join(list_of_strings)

But for just two strings, plus is fine.

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
download_dir=r'c:/download/'
filename =r'log.txt'

I want to get something like this:
c:/download/log.txt
Dec 5 '06 #2

P: n/a
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.
Dec 5 '06 #3

P: n/a
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.
Dec 6 '06 #4

P: n/a
johnny wrote:

Please don't top post. Arrange your answer so that your comments follow what
you comment.
In my code, I have the following:

p = posixpath.basename(e).strip
make this:
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
Which is correct. Because you forgot to _call_ strip, you just stored (a
reference to) strip itself in p, and not its return value.

(rest of message deleted, because I don't comment on it.)

--
Robert Bauck Hamar
Der er to regler for suksess:
1. Fortell aldri alt du vet.
- Roger H. Lincoln
Dec 6 '06 #5

P: n/a

johnny wrote:
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

You need to *call* the strip method.

To see what you've actually done, do this:

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

To see what happens when you've fixed the problem, do this:

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

*and* for portability, *don't* use "posixpath", just use "path"

HTH,
John

Dec 6 '06 #6

P: n/a

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
pathfn = download_dir+filename

Dec 7 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.