473,324 Members | 2,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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

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'

Dec 13 '05 #1
1 3577
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>

Dec 13 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Paul | last post by:
Hi, Using the code below I can extract the filename from a path but I would like to know how to get just the path too. So if the full path is "C:\A Long Time Ago\In A Galaxy\Far Far...
34
by: Reinhold Birkenfeld | last post by:
Hi, the arguments in the previous thread were convincing enough, so I made the Path class inherit from str/unicode again. It still can be found in CVS:...
19
by: Jerry M. Gartner | last post by:
Greetings: What is the best way to resolve paths within a document regardless of what path it is opened under? For example: I have x.php and it contains <img src="images...">, (amongst other...
1
by: Zhiv Kurilka | last post by:
Dear all, supposed I have xml file root> <a> <name>ddd</name> <b> <name>ggg</name> <c>10</c> </b> </a>
0
by: Andrzej Lipski | last post by:
Hello I am developing a Windows Ce 5.0 mobile application. I followed the example shown at : Creating Self-Updating Applications With the .NET Compact Framework...
2
by: Man4ish | last post by:
Hi , How we can find the cyclic path and path between two nodes using Depth first search algorithm in Graph Data Structure. Can i get Pseudo code or code doing the same.I will be very much thankful...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.