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

Discussion in 'Python' started by Jerry Fleming, Mar 27, 2008.

  1. 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.
    Jerry Fleming, Mar 27, 2008
    #1
    1. Advertising

  2. Jerry Fleming

    John Machin Guest

    On Mar 27, 6:41 pm, Jerry Fleming <> 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
    John Machin, Mar 27, 2008
    #2
    1. Advertising

  3. 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
    Wilbert Berendsen, Mar 27, 2008
    #3
  4. 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.
    Bruno Desthuilliers, Mar 27, 2008
    #4
  5. Re: And the reverse? Does os also import os.path?

    En Thu, 27 Mar 2008 14:22:11 -0300, Bruno Desthuilliers
    <> 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
    Gabriel Genellina, Mar 27, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Danny Anderson
    Replies:
    5
    Views:
    498
    Victor Bazarov
    Aug 15, 2003
  2. Andreas Bogenberger
    Replies:
    3
    Views:
    906
    Andreas Bogenberger
    Feb 22, 2008
  3. \A_Michigan_User\

    Various path/path/files.asp all #include this 1 file

    \A_Michigan_User\, Jul 10, 2005, in forum: ASP General
    Replies:
    1
    Views:
    231
    Jeff Cochran
    Jul 10, 2005
  4. Eric
    Replies:
    10
    Views:
    461
  5. Eric
    Replies:
    0
    Views:
    257
Loading...

Share This Page