Wildcard Strings in Path

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Apros
    New Member
    • Nov 2009
    • 4

    Wildcard Strings in Path

    Okay, this is what I need to do, and I keep getting confused.
    I need to remove all files in a folder, then delete the folder, it's part of an installer I'm making, and it creates a directory, I just don't know how to delete it. This is what I have right now:
    curDir = os.getcwd()
    os.remove(os.pa th.join(curDir, *)
    So it will remove all the files in the directory it is in. How do I use the wildcard to make it all files in the directory? And then how do I delete the directory?
    Also, how would I copy the script to another location?

    Any help would be much appreciated!
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    To delete all the files in the current working directory, create a file list with:
    Code:
    os.listdir(os.getcwd())
    Then you can iterate on the list. Delete a directory with:
    Code:
    os.rmdir(path)

    Comment

    • Apros
      New Member
      • Nov 2009
      • 4

      #3
      If I do the first, I end up with:

      Traceback (most recent call last):
      File "<pyshell#1 9>", line 1, in <module>
      os.remove(os.li stdir(os.getcwd ()))
      TypeError: remove() argument 1 must be string, not list

      So how do I make all of the list, which can vary thus needing of a wildcard, into strings? I'm sorry, but I just picked up programming a little while ago, and Python just a couple of weeks ago, so I'm not really sure how it all works.

      If I try the second one, it gives me an error saying it is not able to, because it is in use by another program. How do I make it not do that? And I want to make it so the srcipt deletes everything in the folder, including itself... SOrry if I'm coming off as someone who doesn't know anything. Ireally don't, just need a small more push.

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        When I say iterate on the list:
        Code:
        for fn in fileList:
            os.remove(os.path.join(os.getcwd(), fn)
        Are you trying to remove the current working directory? If so, change directories, then try to delete it.
        Code:
        >>> os.chdir('..')
        >>> os.rmdir('directory_name')
        >>>

        Comment

        • Apros
          New Member
          • Nov 2009
          • 4

          #5
          Grr...
          Okay, this is the script I have so far...

          Code:
          import os
          curDir = os.listdir(os.getcwd())
          for fn in fileList: 
              os.remove(os.path.join(curDir, fn)
          I'm going to guess that I'm doing it completely wrong...
          So... the listdir is supposed to list the files that are in the current directory
          Then for the filename in the list, remove ((Current DIrectory)+File name)
          Correct?
          See, I do that, and it gives me this error:

          Traceback (most recent call last):
          File "<pyshell#1 8>", line 2, in <module>
          os.remove(os.pa th.join(curDir, fn))
          TypeError: remove() argument 1 must be string, not list

          So it appears that I'm making a list of the files, how do I change each object in the list to a string?
          I'm thinking something of doing the list[:]... and that's all I got.
          Again, apologize for the lack of programming knowledge.
          I googled for hours, and couldn't find enough info, except for the RE module, and all the tut's I found were confusing. And it appears that I don't need it.
          Last edited by bvdet; Nov 12 '09, 02:32 AM. Reason: Add code tags

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Uh, try this:
            Code:
            import os
            fileList = os.listdir(os.getcwd())
            for fn in fileList: 
                os.remove(os.path.join(os.getcwd(), fn))
            You were close. Be careful with this code. You could accidentally delete the wrong files.

            Comment

            • Apros
              New Member
              • Nov 2009
              • 4

              #7
              :D
              Thank you, so much!
              It worked!
              *bows to the epic superiority of bvdet*

              Now, if you want to delete the folder, it would be

              path = os.getcwd()
              os.rmdir(path)

              correct, no?

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                Originally posted by Apros
                :D
                Thank you, so much!
                It worked!
                *bows to the epic superiority of bvdet*

                Now, if you want to delete the folder, it would be

                path = os.getcwd()
                os.rmdir(path)

                correct, no?
                You will need to change to another directory. If you attempt to delete the current working directory, you will receive an error that may look like this:

                OSError: [Errno 13] Permission denied: 'directory name whatever'

                Comment

                Working...