By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,089 Members | 2,359 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,089 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Mar 27 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Mar 27, 6:41*pm, Jerry Fleming <jerry.flem...@saybot.comwrote:
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
Mar 27 '08 #2

P: n/a
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
Mar 27 '08 #3

P: n/a
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.

Mar 27 '08 #4

P: n/a
En Thu, 27 Mar 2008 14:22:11 -0300, Bruno Desthuilliers
<br********************@wtf.websiteburo.oops.comes cribió:
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

Mar 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.