Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   do 'os.path' include 'os' for us? (http://www.velocityreviews.com/forums/t602162-do-os-path-include-os-for-us.html)

Jerry Fleming 03-27-2008 07:41 AM

do 'os.path' include 'os' for us?
 
Hi,

I wrote a python script to list files in a directory but somehow did it
wrongly by importing os.path instead of os. To my astonishment, it works
just as charm:
#!/usr/bin/python
import os.path
for file in os.listdir('/root/'):
print file

I was wondering why? os.path doesn't contain listdir, why there is no
complaint like 'os: unknown name'? Does this mean, instead of importing
os, we can import os.path?

Thanks.

John Machin 03-27-2008 08:00 AM

Re: do 'os.path' include 'os' for us?
 
On Mar 27, 6:41*pm, Jerry Fleming <jerry.flem...@saybot.com> wrote:
> Hi,
>
> I wrote a python script to list files in a directory but somehow did it
> wrongly by importing os.path instead of os. To my astonishment, it works
> just as charm:
> #!/usr/bin/python
> import os.path
> for file in os.listdir('/root/'):
> * * * * print file
>
> I was wondering why? os.path doesn't contain listdir, why there is no
> complaint like 'os: unknown name'? Does this mean, instead of importing
> os, we can import os.path?
>


import os.path
in effect imports os, and then os.path, and injects the latter into
the former as an attribute. If it didn't, then when you tried to use
(say) os.path.join, it would raise an exception.

Why don't you do some experimentation at the interactive prompt e.g.
import os.path
type(os)
dir(os)
a = os
a.path
a = nonesuch
# The above will show you what the actual meesage is instead of
complaint like 'os: unknown name' :-)

HTH,
John

Wilbert Berendsen 03-27-2008 01:56 PM

And the reverse? Does os also import os.path?
 
If i do

>>> import os
>>> os.path.abspath("bla")

'/home/wilbert/bla'
>>>


it seems that just import os also makes available al os.path functions.

But is that always true?

Thanks,
Wilbert

--
http://www.wilbertberendsen.nl/
"You must be the change you wish to see in the world."
-- Mahatma Gandhi

Bruno Desthuilliers 03-27-2008 05:22 PM

Re: And the reverse? Does os also import os.path?
 
Wilbert Berendsen a écrit :
> If i do
>
>>>> import os
>>>> os.path.abspath("bla")

> '/home/wilbert/bla'
>
> it seems that just import os also makes available al os.path functions.
>
> But is that always true?


Nope. Not all packages expose their sub-packages.


Gabriel Genellina 03-27-2008 07:05 PM

Re: And the reverse? Does os also import os.path?
 
En Thu, 27 Mar 2008 14:22:11 -0300, Bruno Desthuilliers
<bruno.42.desthuilliers@wtf.websiteburo.oops.com > escribió:

> Wilbert Berendsen a écrit :
>> If i do
>>
>>>>> import os
>>>>> os.path.abspath("bla")

>> '/home/wilbert/bla'
>>
>> it seems that just import os also makes available al os.path functions.
>>
>> But is that always true?

>
> Nope. Not all packages expose their sub-packages.


In this case, the idea is to provide an OS-dependent module with a generic
name. The os module imports one of several modules (ntpath, posixpath,
etc.) depending on the current platform, and makes it available under the
generic "path" name so users don't have to worry about that.

(Note that os is a module, not a package; os.path is accessing the "path"
attribute of the os module, not the path module in the os package)

--
Gabriel Genellina



All times are GMT. The time now is 04:31 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.