[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

problem with join

nodrogbrown

3/7/2008 3:12:00 PM

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

corynissen

3/7/2008 3:34:00 PM

0

On Mar 7, 9:12 am, nodrogbrown <nodrogbr...@gmail.com> 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',
>
> can anyone pls help
> gordon

see path.join in the os library.

Robert Bossy

3/7/2008 3:39:00 PM

0

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

vs.

string.join()
http://docs.python.org/lib/node42.ht...

RB

Tim Golden

3/7/2008 3:41:00 PM

0

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

corynissen

3/7/2008 3:45:00 PM

0

On Mar 7, 9:33 am, corynis...@gmail.com wrote:
> On Mar 7, 9:12 am, nodrogbrown <nodrogbr...@gmail.com> 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',
>
> > 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.

Gabriel Genellina

3/7/2008 3:46:00 PM

0

En Fri, 07 Mar 2008 13:12:13 -0200, nodrogbrown <nodrogbrown@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

nodrogbrown

3/7/2008 4:30:00 PM

0


> 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