How do I remove/unlink wildcarded files

Tim Chase python.list at tim.thechases.com
Fri Jan 2 06:35:52 EST 2015


On 2015-01-02 21:21, Cameron Simpson wrote:
> >def unlinkFiles():
> >    dirname = "/path/to/dir"
> >    for f in os.listdir(dirname):
> >        if re.match("^unix*$", f):
> >            os.remove(os.path.join(dirname, f))
> 
> That is a very expensive way to check the filename in this
> particular case. Consider:
> 
>   if f.startswith('unix'):
> 
> instead of using a regular expression.

And worse, the given re would delete a file named "uni" which doesn't
sound ANYTHING like what the OP wanted :-)

-tkc





More information about the Python-list mailing list