#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    28
    Rep Power
    0

    Files Extensions


    I am having issues with setting an asterisk inside of my code.

    Code:
     if '*.stw' in files:
                      files.remove('*.stw')
    The code basically searches an upload temp folder for files that have the extension of ".stw" and deletes them. The only thing is I am doing the wildcard asterisk wrong because it isn't deleting them. Anyone know what the correct way to use an asterisk in Python is?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    52
    Hi!

    You can use glob for this:
    Code:
    import glob
    
    for f in glob.glob('*.stw'):
        # do something with f
    Regards, mawe

    Comments on this post

    • netytan agrees
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    40
    Rep Power
    11
    i'm pretty new to python so not sure about the wild cards but i would do something like this

    Code:
    for x in list:
    	if x[-4:].lower() == ".stw":
                  os.remove(x)

    Comments on this post

    • monkeyman23555 agrees : yup
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You could also use the endswith() method to replace the slice in the above example:

    Code:
    >>> fileName = 'sample.stw'
    >>> fileName.endswith('.stw')
    True
    >>> fileName.endswith('stw')
    True
    >>>
    That said I would still go with glob in this situation.

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

  8. #5
  9. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    It seems what netytan suggested it easier and quicker. While it does the exact same task that februuscomics needs done.

    PS. I can't seem to agree with any user of the site. No matter who I attempt to agree with, it says I must spread points to other users first.
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    I get the same thing Yegg, what it simply means is that you've already given reputation to this person. This is probably to stop people abusing the system and giving eachother loads and loads of rep .
    programming language development: www.netytan.com Hula

  12. #7
  13. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Oh, I understand now. Thanks.

IMN logo majestic logo threadwatch logo seochat tools logo