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

problem with join

P: n/a
hi
i am using python on WinXP..i have a string 'folder ' that i want to
join to a set of imagefile names to create complete qualified names so
that i can create objects out of them

folder='F:/brown/code/python/fgrp1'
filenms=['amber1.jpg', 'amber3.jpg', 'amy1.jpg', 'amy2.jpg']
filenameslist=[]
for x in filenms:
myfile=join(folder,x)
filenameslist.append(myfile)

now when i print the filenameslist i find that it looks like

['F:/brown/code/python/fgrp1\\amber1.jpg',
'F:/brown/code/python/fgrp1\\amber3.jpg', 'F:/brown/code/python/fgrp1\
\amy1.jpg', 'F:/brown/code/python/fgrp1\\amy2.jpg']

is there some problem with the way i use join? why do i get \\ infront
of the basename?
i would prefer it like 'F:/brown/code/python/fgrp1/basename.jpg',

can anyone pls help
gordon
Mar 7 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Mar 7, 9:12 am, nodrogbrown <nodrogbr...@gmail.comwrote:
hi
i am using python on WinXP..i have a string 'folder ' that i want to
join to a set of imagefile names to create complete qualified names so
that i can create objects out of them

folder='F:/brown/code/python/fgrp1'
filenms=['amber1.jpg', 'amber3.jpg', 'amy1.jpg', 'amy2.jpg']
filenameslist=[]
for x in filenms:
myfile=join(folder,x)
filenameslist.append(myfile)

now when i print the filenameslist i find that it looks like

['F:/brown/code/python/fgrp1\\amber1.jpg',
'F:/brown/code/python/fgrp1\\amber3.jpg', 'F:/brown/code/python/fgrp1\
\amy1.jpg', 'F:/brown/code/python/fgrp1\\amy2.jpg']

is there some problem with the way i use join? why do i get \\ infront
of the basename?
i would prefer it like 'F:/brown/code/python/fgrp1/basename.jpg',

can anyone pls help
gordon
see path.join in the os library.
Mar 7 '08 #2

P: n/a
nodrogbrown wrote:
hi
i am using python on WinXP..i have a string 'folder ' that i want to
join to a set of imagefile names to create complete qualified names so
that i can create objects out of them

folder='F:/brown/code/python/fgrp1'
filenms=['amber1.jpg', 'amber3.jpg', 'amy1.jpg', 'amy2.jpg']
filenameslist=[]
for x in filenms:
myfile=join(folder,x)
filenameslist.append(myfile)

now when i print the filenameslist i find that it looks like

['F:/brown/code/python/fgrp1\\amber1.jpg',
'F:/brown/code/python/fgrp1\\amber3.jpg', 'F:/brown/code/python/fgrp1\
\amy1.jpg', 'F:/brown/code/python/fgrp1\\amy2.jpg']

is there some problem with the way i use join? why do i get \\ infront
of the basename?
i would prefer it like 'F:/brown/code/python/fgrp1/basename.jpg',
os.path.join()
http://docs.python.org/lib/module-os.path.html#l2h-2185

vs.

string.join()
http://docs.python.org/lib/node42.html#l2h-379

RB
Mar 7 '08 #3

P: n/a
nodrogbrown wrote:
hi
i am using python on WinXP..i have a string 'folder ' that i want to
join to a set of imagefile names to create complete qualified names so
that i can create objects out of them

folder='F:/brown/code/python/fgrp1'
filenms=['amber1.jpg', 'amber3.jpg', 'amy1.jpg', 'amy2.jpg']
filenameslist=[]
for x in filenms:
myfile=join(folder,x)
filenameslist.append(myfile)

now when i print the filenameslist i find that it looks like

['F:/brown/code/python/fgrp1\\amber1.jpg',
'F:/brown/code/python/fgrp1\\amber3.jpg', 'F:/brown/code/python/fgrp1\
\amy1.jpg', 'F:/brown/code/python/fgrp1\\amy2.jpg']

is there some problem with the way i use join? why do i get \\ infront
of the basename?
i would prefer it like 'F:/brown/code/python/fgrp1/basename.jpg',
You've got a couple of options. Your "folder" to start
with is in the unixy form a/b/c and the .join function
doesn't do anything to change that, merely uses os.pathsep
to append the parts to each other.

You can either set your folder to be r"f:\brown\code..."
in the first case or use os.path.normpath or os.path.abspath
on the result.

TJG
Mar 7 '08 #4

P: n/a
On Mar 7, 9:33 am, corynis...@gmail.com wrote:
On Mar 7, 9:12 am, nodrogbrown <nodrogbr...@gmail.comwrote:
hi
i am using python on WinXP..i have a string 'folder ' that i want to
join to a set of imagefile names to create complete qualified names so
that i can create objects out of them
folder='F:/brown/code/python/fgrp1'
filenms=['amber1.jpg', 'amber3.jpg', 'amy1.jpg', 'amy2.jpg']
filenameslist=[]
for x in filenms:
myfile=join(folder,x)
filenameslist.append(myfile)
now when i print the filenameslist i find that it looks like
['F:/brown/code/python/fgrp1\\amber1.jpg',
'F:/brown/code/python/fgrp1\\amber3.jpg', 'F:/brown/code/python/fgrp1\
\amy1.jpg', 'F:/brown/code/python/fgrp1\\amy2.jpg']
is there some problem with the way i use join? why do i get \\ infront
of the basename?
i would prefer it like 'F:/brown/code/python/fgrp1/basename.jpg',
can anyone pls help
gordon

see path.join in the os library.
Upon further examination... it looks like you are using windows and
os.path.join.

The separator for windows is a '\'. In python, you have to escape
that character with another '\'. That's why you see '\\'.

That being said, I think what you have will still work to access a
file. Windows is usually smart enough to deal with a front slash here
and there.

Mar 7 '08 #5

P: n/a
En Fri, 07 Mar 2008 13:12:13 -0200, nodrogbrown <no*********@gmail.com>
escribi�:
i am using python on WinXP..i have a string 'folder ' that i want to
join to a set of imagefile names to create complete qualified names so
that i can create objects out of them

folder='F:/brown/code/python/fgrp1'
filenms=['amber1.jpg', 'amber3.jpg', 'amy1.jpg', 'amy2.jpg']
filenameslist=[]
for x in filenms:
myfile=join(folder,x)
filenameslist.append(myfile)

now when i print the filenameslist i find that it looks like

['F:/brown/code/python/fgrp1\\amber1.jpg',
'F:/brown/code/python/fgrp1\\amber3.jpg', 'F:/brown/code/python/fgrp1\
\amy1.jpg', 'F:/brown/code/python/fgrp1\\amy2.jpg']

is there some problem with the way i use join? why do i get \\ infront
of the basename?
join is fine. "\\" is a single character. \ is used as the escape
character, and has to be doubled when representing itself. Print an
individual element to see the effect:

print filenameslist[0]
F:/brown/code/python/fgrp1\amber1.jpg

(a list uses repr() on its elements instead of str()).
i would prefer it like 'F:/brown/code/python/fgrp1/basename.jpg',
If the string is only used to open a file, and never shown to the user,
what you prefer is irrelevant, isn't it?
What is important here is what Windows prefers, and that's a backslash,
although many times / is accepted too. You can convert the file names to
their preferred spelling using os.path.normpath

Back to your code, try this:

from os.path import join, normpath
folder = 'F:/brown/code/python/fgrp1'
names = ['amber1.jpg', 'amber3.jpg', 'amy1.jpg', 'amy2.jpg']
filenameslist = [normpath(join(folder, name)) for name in names]

--
Gabriel Genellina

Mar 7 '08 #6

P: n/a
If the string is only used to open a file, and never shown to the user,
what you prefer is irrelevant, isn't it?
guess thats right..
Back to your code, try this:

from os.path import join, normpath
folder = 'F:/brown/code/python/fgrp1'
names = ['amber1.jpg', 'amber3.jpg', 'amy1.jpg', 'amy2.jpg']
filenameslist = [normpath(join(folder, name)) for name in names]

thanks Gabriel..
gordon
Mar 7 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.