Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > problem with join

Reply
Thread Tools

problem with join

 
 
nodrogbrown
Guest
Posts: n/a
 
      03-07-2008
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
 
Reply With Quote
 
 
 
 
corynissen@gmail.com
Guest
Posts: n/a
 
      03-07-2008
On Mar 7, 9:12 am, nodrogbrown <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Robert Bossy
Guest
Posts: n/a
 
      03-07-2008
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
 
Reply With Quote
 
Tim Golden
Guest
Posts: n/a
 
      03-07-2008
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
 
Reply With Quote
 
corynissen@gmail.com
Guest
Posts: n/a
 
      03-07-2008
On Mar 7, 9:33 am, (E-Mail Removed) wrote:
> On Mar 7, 9:12 am, nodrogbrown <(E-Mail Removed)> 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.

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      03-07-2008
En Fri, 07 Mar 2008 13:12:13 -0200, nodrogbrown <(E-Mail Removed)>
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

 
Reply With Quote
 
nodrogbrown
Guest
Posts: n/a
 
      03-07-2008

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with join in__str__() in class (newbie) Fencer Python 6 08-10-2009 09:23 AM
Problem with Hibernate Join Cacho Java 0 01-18-2007 09:24 AM
You must join New York Paid To Read !!!!!!! NewYork PTR is online!!! JOIN NOW!!!!!! Alan Silver ASP .Net 0 06-05-2006 03:27 PM
list.join()... re.join()...? Do they exist? (newbie questions...) googleboy Python 1 10-01-2005 12:56 PM
Help with Thread.Join() (New Problem) Elizabeth Harmon ASP .Net 1 12-02-2003 06:55 PM



Advertisments