473,386 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

newb: Join two string variables

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
6 11268

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
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
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
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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Amy G | last post by:
I have a whole bunch of tuples that look something like this, aTuple = ('1074545869.6580.msg', 't_bryan_pw@gmcc.ab.ca', 'Your one stop prescriptions') now that I have this I try for x, y, z...
24
by: Apotheosis | last post by:
The problem professor gave us is: Write a program which reads two integer values. If the first is less than the second, print the message "up". If the second is less than the first, print the...
15
by: manstey | last post by:
Hi, I have a text file called a.txt: # comments I read it using this:
8
by: manstey | last post by:
Hi, I often have code like this: data='asdfbasdf' find = (('a','f')('s','g'),('x','y')) for i in find: if i in data: data = data.replace(i,i)
29
by: jaysherby | last post by:
I'm new at Python and I need a little advice. Part of the script I'm trying to write needs to be aware of all the files of a certain extension in the script's path and all sub-directories. Can...
2
by: dave | last post by:
Hi, very new to SQL queries, and strugling with join concept I had to do a join based on a single field: select * from tableA, tableB
2
by: J | last post by:
Greetings Group- I'm trying to put together a pattern matching script that scans a directory tree for tif images contained in similar folder names, but running into a NewB problem already. Is it...
2
by: johnny | last post by:
Scope of ids: When I print "ids", it's always empty string '', as I have intialized before. That's not what I want. I want the ids to have str(r).join(',') if res: ids = '' for r in res:...
1
by: crybaby | last post by:
when I do this in my python code and run it in windows xp, it creates ctemp/..../.../.../../ so on and creates file t. Not file starting with the name complist and ending with .txt...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.