problem with join

Discussion in 'Python' started by nodrogbrown, Mar 7, 2008.

  1. nodrogbrown

    nodrogbrown Guest

    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
    nodrogbrown, Mar 7, 2008
    #1
    1. Advertising

  2. nodrogbrown

    Guest

    On Mar 7, 9:12 am, 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',
    >
    > can anyone pls help
    > gordon


    see path.join in the os library.
    , Mar 7, 2008
    #2
    1. Advertising

  3. nodrogbrown

    Robert Bossy Guest

    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
    Robert Bossy, Mar 7, 2008
    #3
  4. nodrogbrown

    Tim Golden Guest

    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
    Tim Golden, Mar 7, 2008
    #4
  5. nodrogbrown

    Guest

    On Mar 7, 9:33 am, wrote:
    > On Mar 7, 9:12 am, 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',

    >
    > > 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, 2008
    #5
  6. En Fri, 07 Mar 2008 13:12:13 -0200, nodrogbrown <>
    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
    Gabriel Genellina, Mar 7, 2008
    #6
  7. nodrogbrown

    nodrogbrown Guest


    > 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
    nodrogbrown, Mar 7, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Elizabeth Harmon

    Help with Thread.Join() (New Problem)

    Elizabeth Harmon, Dec 2, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    486
    Elizabeth Harmon
    Dec 2, 2003
  2. Alan Silver
    Replies:
    0
    Views:
    877
    Alan Silver
    Jun 5, 2006
  3. googleboy
    Replies:
    1
    Views:
    918
    Benji York
    Oct 1, 2005
  4. Cacho

    Problem with Hibernate Join

    Cacho, Jan 18, 2007, in forum: Java
    Replies:
    0
    Views:
    338
    Cacho
    Jan 18, 2007
  5. Fencer
    Replies:
    6
    Views:
    295
    Fencer
    Aug 10, 2009
Loading...

Share This Page