How do I take a directory name from a given dir?

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

    #1

    How do I take a directory name from a given dir?

    Hi,

    I am trying to write a script to backup all my company's server configs
    weekly. The script will run in a cronjob, on whatever I set it.

    The issue I am currently having isto "extract" the directory name from
    a given directory string. For example: from the string
    "/home/testuser/projects/" I need to extract the "projects" part. The
    problem is that the directory names that needs to be used will never be
    the same lenght, so as far as my (very limited) knowledge stretches,
    slicing and indicing is out of the question.

    Can anybody help me, or give me an idea of what I should look at,
    seeing as I'm seriously stuck. I only started coding at the beginnig of
    the year, and was never interested before that, so my knowlege is
    basically non-existent.

    Thanks in advance for the help :)

  • Daniel Nogradi

    #2
    Re: How do I take a directory name from a given dir?

    > Hi,[color=blue]
    >
    > I am trying to write a script to backup all my company's server configs
    > weekly. The script will run in a cronjob, on whatever I set it.
    >
    > The issue I am currently having isto "extract" the directory name from
    > a given directory string. For example: from the string
    > "/home/testuser/projects/" I need to extract the "projects" part. The
    > problem is that the directory names that needs to be used will never be
    > the same lenght, so as far as my (very limited) knowledge stretches,
    > slicing and indicing is out of the question.
    >
    > Can anybody help me, or give me an idea of what I should look at,
    > seeing as I'm seriously stuck. I only started coding at the beginnig of
    > the year, and was never interested before that, so my knowlege is
    > basically non-existent.[/color]

    You can try this:
    [color=blue][color=green][color=darkred]
    >>> '/home/testuser/projects/'.strip( '/' ).split( '/' )[/color][/color][/color]
    ['home', 'testuser', 'projects']

    strip gets rid of the first and last / and split splits the remaining
    part and puts the results into a list.

    HTH :)

    Comment

    • Merrigan Took

      #3
      Re: How do I take a directory name from a given dir?

      Daniel Nogradi wrote:[color=blue][color=green]
      >> Hi,
      >>
      >> I am trying to write a script to backup all my company's server configs
      >> weekly. The script will run in a cronjob, on whatever I set it.
      >>
      >> The issue I am currently having isto "extract" the directory name from
      >> a given directory string. For example: from the string
      >> "/home/testuser/projects/" I need to extract the "projects" part. The
      >> problem is that the directory names that needs to be used will never be
      >> the same lenght, so as far as my (very limited) knowledge stretches,
      >> slicing and indicing is out of the question.
      >>
      >> Can anybody help me, or give me an idea of what I should look at,
      >> seeing as I'm seriously stuck. I only started coding at the beginnig of
      >> the year, and was never interested before that, so my knowlege is
      >> basically non-existent.[/color]
      >
      > You can try this:
      >[color=green][color=darkred]
      >>>> '/home/testuser/projects/'.strip( '/' ).split( '/' )[/color][/color]
      > ['home', 'testuser', 'projects']
      >
      > strip gets rid of the first and last / and split splits the remaining
      > part and puts the results into a list.
      >
      > HTH :)[/color]
      AAAAh, ok, Thank you, I will definitely look into this and play with it!

      Comment

      • Mike Kent

        #4
        Re: How do I take a directory name from a given dir?

        Python 2.4.2 (#1, Nov 29 2005, 14:04:55)
        [GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
        Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
        >>> import os
        >>> d = "/home/testuser/projects"
        >>> os.path.basenam e(d)[/color][/color][/color]
        'projects'[color=blue][color=green][color=darkred]
        >>> os.path.dirname (d)[/color][/color][/color]
        '/home/testuser'[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        Comment

        • Fredrik Lundh

          #5
          Re: How do I take a directory name from a given dir?

          "Merrigan" wrote:
          [color=blue]
          > I am trying to write a script to backup all my company's server configs
          > weekly. The script will run in a cronjob, on whatever I set it.
          >
          > The issue I am currently having isto "extract" the directory name from
          > a given directory string. For example: from the string
          > "/home/testuser/projects/" I need to extract the "projects" part. The
          > problem is that the directory names that needs to be used will never be
          > the same lenght, so as far as my (very limited) knowledge stretches,
          > slicing and indicing is out of the question.[/color]

          os.path.split(p ath) splits a path into directory name and "base name"
          parts.
          [color=blue][color=green][color=darkred]
          >>> import os
          >>> os.path.split("/usr/bin/python")[/color][/color][/color]
          ('/usr/bin', 'python')

          if you have a trailing slash, split will return an empty string for the base-
          name part
          [color=blue][color=green][color=darkred]
          >>> os.path.split("/home/testuser/projects/")[/color][/color][/color]
          ('/home/testuser/projects', '')

          but you can work around that by doing an extra split:
          [color=blue][color=green][color=darkred]
          >>> path = "/home/testuser/projects/"
          >>> path, name = os.path.split(p ath)
          >>> if not name:[/color][/color][/color]
          ... path, name = os.path.split(p ath)
          ...[color=blue][color=green][color=darkred]
          >>> path[/color][/color][/color]
          '/home/testuser'[color=blue][color=green][color=darkred]
          >>> name[/color][/color][/color]
          'projects'

          hope this helps!

          </F>



          Comment

          • Michael Ekstrand

            #6
            Re: How do I take a directory name from a given dir?

            On Mon, May 01, 2006 at 12:42:58PM -0700, Merrigan wrote:[color=blue]
            > The issue I am currently having isto "extract" the directory name from
            > a given directory string. For example: from the string
            > "/home/testuser/projects/" I need to extract the "projects" part. The
            > problem is that the directory names that needs to be used will never be
            > the same lenght, so as far as my (very limited) knowledge stretches,
            > slicing and indicing is out of the question.[/color]

            Look at the os.path module, especially the basename and dirname
            functions. basename will extract the "base name", that is, the last
            component of a path.

            - Michael

            --
            mouse, n: a device for pointing at the xterm in which you want to type.
            -- Fortune
            Visit me on the Web: http://www.elehack.net

            Comment

            • Merrigan

              #7
              Re: How do I take a directory name from a given dir?

              WOW, Thanks guys. I am really awestruck by the speed you guys relied
              with. Thanks a lot for the help. I eventually found a way that did the
              job for me.

              I will definitely visit this Group more often.

              Thanks for all the help! :D

              Comment

              Working...