os.path.isdir question

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

    os.path.isdir question

    Hello,

    I'm having some problems with os.path.isdir I think it is something
    simple that I'm overlooking.

    #!/usr/bin/python
    import os

    my_path = os.path.expandu ser("~/pictures/")
    print my_path
    results = os.listdir(my_p ath)
    for a_result in results:
    if os.path.isdir(s tr(my_path) + str(a_result)):
    results.remove( a_result)

    for x in results: print x

    The problem is, that the directories are never removed. Can anyone
    point out what I'm missing that is causing the bug? Is there a better
    way of doing this?

    Thanks,
  • Benjamin

    #2
    Re: os.path.isdir question

    On Mar 15, 8:12 pm, lampshade <mwolff...@gmai l.comwrote:
    Hello,
    >
    I'm having some problems with os.path.isdir I think it is something
    simple that I'm overlooking.
    >
    #!/usr/bin/python
    import os
    >
    my_path = os.path.expandu ser("~/pictures/")
    print my_path
    results = os.listdir(my_p ath)
    for a_result in results:
    if os.path.isdir(s tr(my_path) + str(a_result)):
    Try if os.path.isdir(o s.path.join(my_ path, a_result)):
    results.remove( a_result)
    >
    for x in results: print x
    >
    The problem is, that the directories are never removed. Can anyone
    point out what I'm missing that is causing the bug? Is there a better
    way of doing this?
    You should always use os.path.join to join paths. You shouldn't add
    them like normal strings. I suspect you're getting a combination which
    doesn't exist, so it isn't a dir. :)
    >
    Thanks,

    Comment

    • yoz

      #3
      Re: os.path.isdir question

      lampshade wrote:
      Hello,
      >
      I'm having some problems with os.path.isdir I think it is something
      simple that I'm overlooking.
      >
      #!/usr/bin/python
      import os
      >
      my_path = os.path.expandu ser("~/pictures/")
      print my_path
      results = os.listdir(my_p ath)
      for a_result in results:
      if os.path.isdir(s tr(my_path) + str(a_result)):
      results.remove( a_result)
      >
      for x in results: print x
      >
      The problem is, that the directories are never removed. Can anyone
      point out what I'm missing that is causing the bug? Is there a better
      way of doing this?
      >
      Thanks,
      Your code works fine for me ..

      Someone will probably complain that this isn't pythonic or hopefully
      come up with an even neater way but what about this:

      #!/usr/bin/python
      import os

      my_path = os.path.expandu ser("~/pictures/")
      print my_path
      files=os.walk(m y_path).next()[2]
      for x in files: print x

      EdH.

      Comment

      • lampshade

        #4
        Re: os.path.isdir question

        On Mar 15, 9:27 pm, Benjamin <musiccomposit. ..@gmail.comwro te:
        On Mar 15, 8:12 pm, lampshade <mwolff...@gmai l.comwrote:Hell o,
        >
        I'm having some problems with os.path.isdir I think it is something
        simple that I'm overlooking.
        >
        #!/usr/bin/python
        import os
        >
        my_path = os.path.expandu ser("~/pictures/")
        print my_path
        results = os.listdir(my_p ath)
        for a_result in results:
        if os.path.isdir(s tr(my_path) + str(a_result)):
        >
        Try if os.path.isdir(o s.path.join(my_ path, a_result)): results.remove( a_result)
        >
        for x in results: print x
        >
        The problem is, that the directories are never removed. Can anyone
        point out what I'm missing that is causing the bug? Is there a better
        way of doing this?
        >
        You should always use os.path.join to join paths. You shouldn't add
        them like normal strings. I suspect you're getting a combination which
        doesn't exist, so it isn't a dir. :)
        >
        >
        >
        Thanks,
        Thanks, that nailed it perfectly! I'll remember the os.path.join from
        now on!

        Comment

        • John Machin

          #5
          Re: os.path.isdir question

          On Mar 16, 12:12 pm, lampshade <mwolff...@gmai l.comwrote:
          Hello,
          >
          I'm having some problems with os.path.isdir I think it is something
          simple that I'm overlooking.
          >
          #!/usr/bin/python
          import os
          >
          my_path = os.path.expandu ser("~/pictures/")
          print my_path
          results = os.listdir(my_p ath)
          for a_result in results:
          if os.path.isdir(s tr(my_path) + str(a_result)):
          Not parts of the problem, but:
          (1) you don't need to use str() here.
          (2) use os.path.join instead of the + operator, if you are interested
          in portable code.
          results.remove( a_result)
          >
          for x in results: print x
          >
          The problem is, that the directories are never removed. Can anyone
          point out what I'm missing that is causing the bug? Is there a better
          way of doing this?
          >
          Thanks,
          You are removing directory *NAMES* from the container named "results".
          If you want to remove the directories from your filesystem, use
          os.rmdir.

          Comment

          • MRAB

            #6
            Re: os.path.isdir question

            On Mar 16, 2:27 am, Benjamin <musiccomposit. ..@gmail.comwro te:
            On Mar 15, 8:12 pm, lampshade <mwolff...@gmai l.comwrote:Hell o,
            >
            I'm having some problems with os.path.isdir I think it is something
            simple that I'm overlooking.
            >
            #!/usr/bin/python
            import os
            >
            my_path = os.path.expandu ser("~/pictures/")
            print my_path
            results = os.listdir(my_p ath)
            for a_result in results:
            if os.path.isdir(s tr(my_path) + str(a_result)):
            >
            Try if os.path.isdir(o s.path.join(my_ path, a_result)): results.remove( a_result)
            >
            for x in results: print x
            >
            The problem is, that the directories are never removed. Can anyone
            point out what I'm missing that is causing the bug? Is there a better
            way of doing this?
            >
            You should always use os.path.join to join paths. You shouldn't add
            them like normal strings. I suspect you're getting a combination which
            doesn't exist, so it isn't a dir. :)
            >
            You are also removing items from 'results' while iterating over it,
            which has an undefined behaviour. It would be better to build a new
            list of those that aren't directories.

            Comment

            • Benjamin

              #7
              Re: os.path.isdir question

              On Mar 16, 2:27 pm, MRAB <goo...@mrabarn ett.plus.comwro te:
              On Mar 16, 2:27 am, Benjamin <musiccomposit. ..@gmail.comwro te:
              >
              On Mar 15, 8:12 pm, lampshade <mwolff...@gmai l.comwrote:Hell o,
              >
              I'm having some problems with os.path.isdir I think it is something
              simple that I'm overlooking.
              >
              #!/usr/bin/python
              import os
              >
              my_path = os.path.expandu ser("~/pictures/")
              print my_path
              results = os.listdir(my_p ath)
              for a_result in results:
              if os.path.isdir(s tr(my_path) + str(a_result)):
              >
              Try if os.path.isdir(o s.path.join(my_ path, a_result)): results.remove( a_result)
              >
              for x in results: print x
              >
              The problem is, that the directories are never removed. Can anyone
              point out what I'm missing that is causing the bug? Is there a better
              way of doing this?
              >
              You should always use os.path.join to join paths. You shouldn't add
              them like normal strings. I suspect you're getting a combination which
              doesn't exist, so it isn't a dir. :)
              >
              You are also removing items from 'results' while iterating over it,
              which has an undefined behaviour. It would be better to build a new
              list of those that aren't directories.
              This list comprehension should do the trick pythonically:
              final_results = [a_result for a_result in results if
              os.path.isdir(o s.path.join(my_ path, a_result))]

              Comment

              Working...