How do I remove/unlink wildcarded files

Ervin Hegedüs airween at gmail.com
Fri Jan 2 07:36:04 EST 2015


Hi,
On Fri, Jan 02, 2015 at 05:35:52AM -0600, Tim Chase wrote:
> 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 :-)

yes, you're right - I've missed out a "." before the "*". :)
thanks:
a.
-- 
I � UTF-8


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /