Why does os.path.join('/foo', '/bar') return '/bar' rather than
'/foo/bar'? That just seems rather counter intuitive.
Elliot 10 11772
On May 1, 7:36 pm, Elliot Peele <ell...@bentlog ic.netwrote:
Why does os.path.join('/foo', '/bar') return '/bar' rather than
'/foo/bar'? That just seems rather counter intuitive.
Elliot
join( path1[, path2[, ...]])
Join one or more path components intelligently. If any component is an
absolute path, all previous components (on Windows, including the
previous drive letter, if there was one) are thrown away...
On Tue, 2007-05-01 at 19:27 -0700, 7stud wrote:
On May 1, 7:36 pm, Elliot Peele <ell...@bentlog ic.netwrote:
Why does os.path.join('/foo', '/bar') return '/bar' rather than
'/foo/bar'? That just seems rather counter intuitive.
Elliot
join( path1[, path2[, ...]])
Join one or more path components intelligently. If any component is an
absolute path, all previous components (on Windows, including the
previous drive letter, if there was one) are thrown away...
Yes, but that still doesn't answer my question as to why os.path.join
works that way. I understand that that is how it is written, but why?
Elliot
On May 1, 9:23 pm, Elliot Peele <ell...@bentlog ic.netwrote:
On Tue, 2007-05-01 at 19:27 -0700, 7stud wrote:
On May 1, 7:36 pm, Elliot Peele <ell...@bentlog ic.netwrote:
Why does os.path.join('/foo', '/bar') return '/bar' rather than
'/foo/bar'? That just seems rather counter intuitive.
Elliot
join( path1[, path2[, ...]])
Join one or more path components intelligently. If any component is an
absolute path, all previous components (on Windows, including the
previous drive letter, if there was one) are thrown away...
Yes, but that still doesn't answer my question as to why os.path.join
works that way. I understand that that is how it is written, but why?
Elliot
It makes perfect sense. You are joining two paths that both begin at
the root directory. The second path is overwriting the first because
they can't both begin at the root and also be parts of one path.
A better question is why this doesn't work.
>>pathparts = ["/foo", "bar"] os.path.join( pathparts)
['/foo', 'bar']
This should return a string in my opinion.
~Sean
En Wed, 02 May 2007 01:23:45 -0300, Elliot Peele <el****@bentlog ic.net>
escribió:
On Tue, 2007-05-01 at 19:27 -0700, 7stud wrote:
>On May 1, 7:36 pm, Elliot Peele <ell...@bentlog ic.netwrote:
Why does os.path.join('/foo', '/bar') return '/bar' rather than
'/foo/bar'? That just seems rather counter intuitive.
Elliot
join( path1[, path2[, ...]]) Join one or more path components intelligently. If any component is an absolute path, all previous components (on Windows, including the previous drive letter, if there was one) are thrown away...
Yes, but that still doesn't answer my question as to why os.path.join
works that way. I understand that that is how it is written, but why?
It's not *how* it is written, but the current documentation for
os.path.join: http://docs.python.org/lib/module-os.path.html#l2h-2176
It appears that the function docstring (used by the help system) is too
terse here.
--
Gabriel Genellina
En Wed, 02 May 2007 02:31:43 -0300, <ha**********@g mail.comescribi ó:
A better question is why this doesn't work.
>>>pathparts = ["/foo", "bar"] os.path.join (pathparts)
['/foo', 'bar']
This should return a string in my opinion.
I think it's a bug, but because it should raise TypeError instead.
The right usage is os.path.join(*p athparts)
--
Gabriel Genellina
On May 1, 11:10 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
En Wed, 02 May 2007 02:31:43 -0300, <half.ital...@g mail.comescribi ó:
A better question is why this doesn't work.
>>pathparts = ["/foo", "bar"] os.path.join( pathparts)
['/foo', 'bar']
This should return a string in my opinion.
I think it's a bug, but because it should raise TypeError instead.
The right usage is os.path.join(*p athparts)
--
Gabriel Genellina
Wow. What exactly is that * operator doing? Is it only used in
passing args to functions? Does it just expand the list into
individual string arguments for exactly this situation? Or does it
have other uses?
~Sean
En Wed, 02 May 2007 04:03:56 -0300, <ha**********@g mail.comescribi ó:
On May 1, 11:10 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
>The right usage is os.path.join(*p athparts)
Wow. What exactly is that * operator doing? Is it only used in
passing args to functions? Does it just expand the list into
individual string arguments for exactly this situation? Or does it
have other uses?
When calling a function, it is used to pass a sequence as positional
arguments. Similarly, **values is used to pass a dictionary as keyword
arguments.
When defining a function, *args receives the remaining positional
arguments not already bound to another parameter; and **kwargs receives
the remaining keyword arguments not already bound to another parameter.
[There is nothing special on the *args and **kwargs names, only the * and
** are important]
See section 4.7 on the Python Tutorial http://docs.python.org/tut/node6.htm...00000000000000 and
specially section 4.7.4 Unpacking Argument Lists.
For a more technical description (but sometimes necesary) read the Python
Reference Manual http://docs.python.org/ref/calls.html
--
Gabriel Genellina
On May 2, 8:03 am, half.ital...@gm ail.com wrote:
On May 1, 11:10 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
....
I think it's a bug, but because it should raise TypeError instead.
The right usage is os.path.join(*p athparts)
....
Wow. What exactly is that * operator doing? Is it only used in
passing args to functions? Does it just expand the list into
individual string arguments for exactly this situation? Or does it
have other uses?
It's used for unpacking a collection into arguments to a function.
It's also used at the other end for receiving a variable length set of
arguments. i.e.
>>x = (1,3) def add(a, b):
return a + b
>>add(*x)
4
>>def add(*args):
return reduce(int.__ad d__, args)
>>add(1,2,3,4,5 ,6)
21
>>add(*x)
4
The same sort of thing holds for keyword arguments:
>>def print_kw(**kw):
for k in kw:
print kw[k]
>>print_kw(a= 1, b=2)
1
2
>>d = {'a': 1, 'b': 10, 'c': 100} print_kw(** d)
1
100
10
On May 2, 12:36 am, Ant <ant...@gmail.c omwrote:
On May 2, 8:03 am, half.ital...@gm ail.com wrote:
On May 1, 11:10 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
...
I think it's a bug, but because it should raise TypeError instead.
The right usage is os.path.join(*p athparts)
...
Wow. What exactly is that * operator doing? Is it only used in
passing args to functions? Does it just expand the list into
individual string arguments for exactly this situation? Or does it
have other uses?
It's used for unpacking a collection into arguments to a function.
It's also used at the other end for receiving a variable length set of
arguments. i.e.
>x = (1,3) def add(a, b):
return a + b
>add(*x)
4
>def add(*args):
return reduce(int.__ad d__, args)
>add(1,2,3,4,5, 6)
21
>add(*x)
4
The same sort of thing holds for keyword arguments:
>def print_kw(**kw):
for k in kw:
print kw[k]
>print_kw(a=1 , b=2)
1
2>>d = {'a': 1, 'b': 10, 'c': 100}
>print_kw(**d )
1
100
10
Thank you both. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Pierre Fortin |
last post by:
This quest for understanding started very innocently...
A simple error on my part, passing on args as "args" instead of "*args" to
os.path.join() led me to wonder why an error wasn't raised...
def foo(*args):
...
return os.path.join(*args)
foo('a','b') # returns 'a/b'
|
by: Earl Eiland |
last post by:
os.path.getsize(Inputdirectory + '\\' + Filename) works, but
os.path.getsize(Inputdirectory + '\\' + Filename.split('.') + '.ext')
Fails reporting "no such file or directory
InputDirectory\\Filename.ext".
os.path.getsize(Inputdirectory + r'\' + Filename.split('.') + '.ext')
generates a syntax error.
Earl Eiland
|
by: Michael Hoffman |
last post by:
Many of you are familiar with Jason Orendorff's path module
<http://www.jorendorff.com/articles/python/path/>, which is frequently
recommended here on c.l.p. I submitted an RFE to add it to the Python
standard library, and Reinhold Birkenfeld started a discussion on it in
python-dev...
|
by: Steve |
last post by:
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
|
by: Gregory Piñero |
last post by:
Would someone mind explaining this seemingly strange behavior of
os.path.join to me?
>>> os.path.join('C:\\Documents and
Settings\\Gregory','\\graphics\\knight\\been hit e0001.bmp')
>>'\\graphics\\knight\\been hit e0001.bmp'
And what is the right way to reference this path relatively?
--
| |
by: funkyj |
last post by:
I want to call os.path.join() on a list instead of a variable list of
arguments. I.e.
(186:0)$ python
iPython 2.4 (#2, Feb 18 2005, 16:39:27)
] on freebsd4
Type "help", "copyright", "credits" or "license" for more
information.
m>>>
'/tmp/a/b/c/d'
|
by: Paul Scott |
last post by:
Today, I needed to concatenate a bunch of directory paths and files
together based on user input to create file paths. I achieved this
through nested os.path.join()'s which I am unsure if this is a good
thing or not.
example:
if os.path.exists(os.path.join(basedir,picdir)) == True :
blah blah
|
by: Jean-Paul Calderone |
last post by:
On Mon, 05 May 2008 16:28:33 +0200, Paul Scott <pscott@uwc.ac.zawrote:
How about not nesting the calls?
True
Jean-Paul
|
by: kj |
last post by:
How can a script know its absolute path? (__file__ only gives the
path it was used to invoke the script.)
Basically, I'm looking for the Python equivalent of Perl's FindBin.
The point of all this is to make the scripts location the reference
point for the location of other files, as part of a self-contained
distribution.
TIA!
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |