#1
  1. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155

    Wildcard Question


    I'm trying to make a program that simply deletes every file in C:\Documents and Settings\All Users\Favorites. Since windowd does not along me to remove C:\Documents and Settings\All Users\Favorites and re-create it, I want to remove every file from it. The best way I could think of was to use wildcards and do *a*, *b*, *c*, etc. So that it would remove any files with these characters. I was trying to use os.remove(), only I just got invalid argument errors. What ways are there that I can just remove all of the files in C:\Documents and Settings\All Users\Favorites?
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    If you want to only remove files that match the supplied pattern you should check out the glob module:

    http://www.python.org/doc/2.4/lib/module-glob.html

    However in this situation it would be much easier to loop though all of the files in the directory using something like os.listdir() or os.walk(), and call os.remove() for each file you wish to remove.

    os.remove() takes only one argument, the path to the file you want to remove, which must be an existing file. If this doesn't work for you then you may wish to post the Traceback/Error for us to look at.

    http://www.python.org/doc/2.4/lib/module-os.html

    Have fun,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    Ok, I still have a big problem. The way I need to remove these files. Python will not (as so it seems) allow me to do so.
    Here's what Im doing.
    Code:
    files = os.listdir('C:\Documents and Settings\All Users\Favorites')
    file = open('remove.bat', 'w')
    file.close()
    for item in files:
        file = open('remove.bat', 'a')
        file.write('\ndel C:\Documents and Settings\All Users\Favorites\+item)
        file.close()
    Now of course I can't do that. The \ after Favorites seems to be causing some kind of a problem. Is there a way to fix this
    ? So that it would write to remove.bat,
    Code:
    del C:\Documents and Settings\All Users\Favorites\microsoft.url
    del C:\Documents and Settings\All Users\Favorites\yahoo.com.url
    And so on?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    Use \\ or / for the folder separators, then it should work from Python.

    \ is a way of marking the next character as a special character - like \n for a newline - \d, \a, \m, \f could all represent special characters and are probably causing your problems.


    (Edit: Also, you aren't closing the string with a ' before doing +item in the line:
    Code:
    file.write('\ndel C:\Documents and Settings\All Users\Favorites\+item)
    )
    Last edited by sfb; February 6th, 2005 at 07:44 PM.
  8. #5
  9. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    Thanks sfb. It works perfectly now. I didn't know I needed to have two \'s.

IMN logo majestic logo threadwatch logo seochat tools logo