Learning Python - Have Question.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AlbaClause

    #1

    Learning Python - Have Question.

    I'm just learning Python, and I have a question about os.path.join(di rpath,
    name) and its use. Simply put, I haven't figured out how to use it.

    I was looking through the Python reference material in the wee hours of the
    morning and checking out some of the modules. I was keenly interested in
    the os module, as it is necessary for me to learn this stuff in order to
    begin my first real Python project.

    I was looking at os.walk(top) when I read a little blurb about using
    os.path.join(di rpath, name) to get complete directory listings with
    path/filname. Unfortunately, I was unable to figure out how to use it. I
    kept getting an error with a message something like "b.startswi th" or
    something like that. I was also unable to find any other information about
    this in the reference material.

    If someone could offer some insight into the use of os.path.join(di rpath,
    name), I would really appreciate it.

    Oh, I have one more question. So far everything that I've played with
    yields only the filename of file. I am aware that os.walk will place the
    pathnames and filenames in a tuple, but I'm wondering if there is a way to
    input a full path directory from within Python. Specifically, I want to be
    able to get directories like one would get by entering "ls -l" at a *nix
    shell prompt.

    Thanks.

    --
    --
    There are several things that I will never be:
    * I will never be attracted to females.
    * I will never enjoy the company of others.
    Exactly how these realities bode for my enemy, is not of my concern.

  • iapain

    #2
    Re: Learning Python - Have Question.

    I'm just learning Python, and I have a question about os.path.join(di rpath,
    name) and its use. Simply put, I haven't figured out how to use it.
    First thing you have to remember while using python is "everything is
    an object". os.join.path concatenates one or more path for example
    os.path.join("c :", "myfolder") represent a path relative to current dir
    on c: drive.
    I was looking through the Python reference material in the wee hours of the
    morning and checking out some of the modules. I was keenly interested in
    the os module, as it is necessary for me to learn this stuff in order to
    begin my first real Python project.
    Dont worry Python is quite easy to learn, just keep on coding in
    python.
    Oh, I have one more question. So far everything that I've played with
    yields only the filename of file. I am aware that os.walk will place the
    pathnames and filenames in a tuple, but I'm wondering if there is a way to
    input a full path directory from within Python. Specifically, I want to be
    able to get directories like one would get by entering "ls -l" at a *nix
    shell prompt.
    you could easily do it with python. Its more than your expectation. The
    best would be to call os.system(shell cmd). You can also print tuple in
    your own way.

    Cheers!
    Deepak

    Comment

    • AlbaClause

      #3
      Re: Learning Python - Have Question.

      iapain wrote:
      >I'm just learning Python, and I have a question about
      >os.path.join(d irpath,
      >name) and its use. Simply put, I haven't figured out how to use it.
      >
      First thing you have to remember while using python is "everything is
      an object". os.join.path concatenates one or more path for example
      os.path.join("c :", "myfolder") represent a path relative to current dir
      on c: drive.
      >
      Thank you. I was thinking about it all wrong. That helped a lot. I have
      it figured out now. :-)



      --
      --
      There are several things that I will never be:
      * I will never be attracted to females.
      * I will never enjoy the company of others.
      Exactly how these realities bode for my enemy, is not of my concern.

      Comment

      • Tal Einat

        #4
        Re: Learning Python - Have Question.


        iapain wrote:
        I'm just learning Python, and I have a question about os.path.join(di rpath,
        name) and its use. Simply put, I haven't figured out how to use it.
        >
        First thing you have to remember while using python is "everything is
        an object". os.join.path concatenates one or more path for example
        os.path.join("c :", "myfolder") represent a path relative to current dir
        on c: drive.
        >
        Actually, os.path.join() is a simple function, there's nothing
        Obejct-Oriented about it. "Everything is an object" simply means that
        functions are objects, but that doesn't mean that the design of
        everything is Object-Oriented.
        Oh, I have one more question. So far everything that I've played with
        yields only the filename of file. I am aware that os.walk will place the
        pathnames and filenames in a tuple, but I'm wondering if there is a way to
        input a full path directory from within Python. Specifically, I want to be
        able to get directories like one would get by entering "ls -l" at a *nix
        shell prompt.
        >
        you could easily do it with python. Its more than your expectation. The
        best would be to call os.system(shell cmd).
        Using the shell for this is the least cross-platform solution possible.
        Python's libraries have very corss-platform implementations of such
        operations you should use them!

        In this case, it seems you're looking for os.listdir(dir_ name). Just
        call it with the path of a directory and it will return a list of names
        of all the files and directories therein. You can use os.path.isdir() ,
        os.path.isfile( ), etc. to figure out what type of file system object
        each item in the list is. You can check premissions for the current
        user with os.access(). Finally, you can find out more data about any
        file with the "stat" module, which is more low-level.

        - Tal Einat
        reduce(lambda m,x:[m[i]+s[-1] for i,s in enumerate(sorte d(m))],
        [[chr(154-ord(c)) for c in '.&-&,l.Z95193+1 79-']]*18)[3]

        Comment

        • AlbaClause

          #5
          Re: Learning Python - Have Question.

          Tal Einat wrote:
          iapain wrote:
          >First thing you have to remember while using python is "everything is
          >an object". os.join.path concatenates one or more path for example
          >os.path.join(" c:", "myfolder") represent a path relative to current dir
          >on c: drive.
          >>
          >
          Actually, os.path.join() is a simple function, there's nothing
          Obejct-Oriented about it. "Everything is an object" simply means that
          functions are objects, but that doesn't mean that the design of
          everything is Object-Oriented.
          It turns out that I was using os.path.join() properly in the first place.
          And you're right, it is a very simple function. Apparently, I was using
          os.walk() improperly. More specifically, I was not fully understanding how
          os.walk() worked. I've played with it some more in the Python interpreter
          and I now have a very good understanding of how it works.

          For example:

          import os
          d=os.walk('/server1')
          for a, b, c in d:
          pass

          This will place the path '/server1' in 'a', all directories in 'a' in a list
          called 'b', and all files in a list called 'c'. On the next iteration of
          the loop, 'a' will contain the path '/server1' with the path of the first
          directory in '/server1' appended. So 'a' will be '/server1/directory1',
          all of the directories in 'a' will be stored in 'b', and all of the files
          in 'a' will be stored in 'c'.

          So on the first iteration, a, b, and c would be:

          a = server1
          b = directory1, directory2, directory3
          c = file1, file2, file3

          On the second iteration of the loop, a, b, and c would be:

          a = server1/directory1
          b = subdirectory1, subdirectory2
          c = d1file1, d1file2, d1file3

          On the third iteration of the loop, a, b, and c would be:

          a = server1/directory1/subdirectory1
          b = [] # there are no directories in subdirectory1 so b is empty.
          c = sd1file1, sdfile2

          >you could easily do it with python. Its more than your expectation. The
          >best would be to call os.system(shell cmd).
          >
          Using the shell for this is the least cross-platform solution possible.
          Python's libraries have very corss-platform implementations of such
          operations you should use them!
          >
          In this case, it seems you're looking for os.listdir(dir_ name). Just
          call it with the path of a directory and it will return a list of names
          of all the files and directories therein. You can use os.path.isdir() ,
          os.path.isfile( ), etc. to figure out what type of file system object
          each item in the list is. You can check premissions for the current
          user with os.access(). Finally, you can find out more data about any
          file with the "stat" module, which is more low-level.
          Thank you for this. The most daunting task in learning Python, is learning
          all of the modules and functions that are available. And there's a tonne
          of them. :-)

          --
          --
          There are several things that I will never be:
          * I will never be attracted to females.
          * I will never enjoy the company of others.
          Exactly how these realities bode for my enemy, is not of my concern.

          Comment

          • Tal Einat

            #6
            Re: Learning Python - Have Question.

            Thank you for this. The most daunting task in learning Python, is learning
            all of the modules and functions that are available. And there's a tonne
            of them. :-)
            >
            Actually, much of this file-system related stuff really is badly spread
            out between many different modules (os, os.path, glob, fnmatch, shutil,
            stat), mostly for historical reasons. Consulting the docs often and
            viewing working code examples are a good way to learn how to
            effectively do these things with Python's current stdlib.

            Some discussion has been going on for several years now about joining
            all such functionality into one "path" module, by creating a standard
            Path object, and having all of this nicely Object-Oriented. Currently
            there is a PEP about this (see PEP 355), but with the release of Python
            2.5 nearing this has been further delayed by the development community.

            PEP 355: http://www.python.org/dev/peps/pep-0355/

            - Tal

            Comment

            Working...