473,738 Members | 9,555 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11311

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+fi lename
path_to_fil e
'C:/download/log.txt'

2.)
>>import os
filename='log .txt'
path_to_fil e = os.path.join("C :/download",filen ame)
path_to_fil e
'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.basen ame(e).strip
filename = download_dir+p

I am getting the following error:

filename = download_dir+p
TypeError: cannot concatenate 'str' and 'builtin_functi on_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=d ownload_dir+fil ename
path_to_file
'C:/download/log.txt'

2.)
>import os
filename='log. txt'
path_to_file = os.path.join("C :/download",filen ame)
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.basen ame(e).strip
make this:
p = posixpath.basen ame(e).strip()
filename = download_dir+p

I am getting the following error:

filename = download_dir+p
TypeError: cannot concatenate 'str' and 'builtin_functi on_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.basen ame(e).strip
filename = download_dir+p

I am getting the following error:

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

You need to *call* the strip method.

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

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

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

p = posixpath.basen ame(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+fi lename

Dec 7 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1561
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 in aTuple: do something with x do something with y
24
2369
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 message "down" If the numbers are equal, print the message "equal" If there is an error reading the data, print a message containing the word "Error" and perform exit( 0 ); And this is what I wrote:
15
3664
by: manstey | last post by:
Hi, I have a text file called a.txt: # comments I read it using this:
8
1188
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
2654
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 someone set me on the right path to what modules and calls to use to do that? You'd think that it would be a fairly simple proposition, but I can't find examples anywhere. Thanks.
2
1830
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
1399
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 the way I'm trying to join multiple paths? Any help would be greatly appericated. Thanks, J! import glob, sys, os topdir = sys.argv
2
1066
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: ids = str(r).join(',')
1
1832
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 (complist*.txt). Any idea why this may be? glob only works in *nix not on windows? os.renames(glob.glob('complist*.txt') ,r'temp/'.join(glob.glob('complist*.txt')))
0
8969
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9476
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9335
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.