*tuple vs tuple example print os.path.join(os.path.dirname(os.tmpnam()),*("a","b","c"))

Discussion in 'Python' started by Steve, Dec 13, 2005.

  1. Steve

    Steve Guest

    I have been trying to find documentation on the behavior
    Can anyone tell me why the first example works and the second doesn't
    and where I can read about it in the language reference?
    Steve

    print os.path.join(os.path.dirname(os.tmpnam()),*("a","b","c"))
    #works
    OUTPUT:/var/tmp/a/b/c
    and

    print os.path.join(os.path.dirname(os.tmpnam()),("a","b","c")) #
    doesn't
    OUTPUT:Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File
    "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/posixpath.py",
    line 60, in join
    if b.startswith('/'):
    AttributeError: 'tuple' object has no attribute 'startswith'
     
    Steve, Dec 13, 2005
    #1
    1. Advertising

  2. Re: *tuple vs tuple exampleprintos.path.join(os.path.dirname(os.tmpnam()), *("a", "b", "c"))

    Steve wrote:

    > I have been trying to find documentation on the behavior


    the behaviour of what ?

    > Can anyone tell me why the first example works and the second doesn't
    > and where I can read about it in the language reference?


    the os.path.join documentation (in the library reference) says

    join(path1[, path2[, ...]])

    Joins one or more path components intelligently. /.../ The return
    value is the concatenation of path1, and optionally path2, etc.,
    with exactly one directory separator (os.sep) inserted between
    components /.../

    that is, the syntax is

    path = os.path.join("part1", "part2", "part3")

    your first example

    > print os.path.join(os.path.dirname(os.tmpnam()),*("a","b","c"))


    is equivalent to

    print os.path.join(os.path.dirname(os.tmpnam()), "a", "b", "c")

    which matches the description in the library reference.

    the "*" notation is described, among other places, in the "calls"
    section of the language reference:

    http://docs.python.org/ref/calls.html

    "If the syntax "*expression" appears in the function call,
    "expression" must evaluate to a sequence. Elements from
    this sequence are treated as if they were additional
    positional arguments"


    you second example

    > print os.path.join(os.path.dirname(os.tmpnam()),("a","b","c")) #


    passes in a tuple as path2, which doesn't match the library reference.
    since Python expects you to pass in a string, you get an exception when
    you pass in something else.

    </F>
     
    Fredrik Lundh, Dec 13, 2005
    #2
    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. Serve Laurijssen

    tmpnam question

    Serve Laurijssen, Feb 23, 2004, in forum: C Programming
    Replies:
    18
    Views:
    607
    Chris Torek
    Feb 25, 2004
  2. Josh Wilson

    tmpnam, strcpy - incompatible pointer type?

    Josh Wilson, Jun 21, 2004, in forum: C Programming
    Replies:
    1
    Views:
    3,835
    Eric Sosman
    Jun 21, 2004
  3. Jason
    Replies:
    3
    Views:
    451
    Jason
    Apr 17, 2004
  4. Harold Fellermann

    how to switch from os.tmpnam to os.tmpfile

    Harold Fellermann, Jun 8, 2006, in forum: Python
    Replies:
    7
    Views:
    1,274
    Nick Craig-Wood
    Jun 8, 2006
  5. happykid
    Replies:
    6
    Views:
    652
    WaffleSouffle
    Aug 9, 2011
Loading...

Share This Page