On Mar 27, 6:41 pm, Jerry Fleming <[EMAIL PROTECTED]> 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
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to