Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'filename': 0.09; 'subject:files': 0.09; 'subject:How': 0.10; 'def': 0.12; 'jan': 0.12; '>>on': 0.16; 'dirname': 0.16; 'expression.': 0.16; 'filename.': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'simpson': 0.16; 'subject:remove': 0.16; 'wrote:': 0.18; 'module': 0.19; 'thu,': 0.19; '>>>': 0.22; 'shell': 0.22; 'header:User- Agent:1': 0.23; 'case.': 0.24; 'directory.': 0.24; "i've": 0.25; 'suggested': 0.26; 'this:': 0.26; 'subject:/': 0.26; 'header:In- Reply-To:1': 0.27; 'tried': 0.27; 'function': 0.29; 'generally': 0.29; "doesn't": 0.30; "i'm": 0.30; 'probably': 0.32; 'regular': 0.32; 'but': 0.35; "didn't": 0.36; 'skip:o 20': 0.38; 'others.': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'expensive': 0.39; 'delete': 0.39; 'to:addr:python.org': 0.39; 'skip:u 10': 0.60; 'skip:o 30': 0.61; '2015': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=ZuVjKrLG c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=xSRs65CQCjkA:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=pGLkceISAAAA:8 a=Vj6Owa0d8VOmQynWp0wA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett:2500 Date: Fri, 02 Jan 2015 11:29:07 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: How do I remove/unlink wildcarded files References: <20150102090051.GC22372@arxnet.hu> <20150102102153.GA89926@cskk.homeip.net> In-Reply-To: <20150102102153.GA89926@cskk.homeip.net> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1420198157 news.xs4all.nl 2843 [2001:888:2000:d::a6]:32803 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83112 On 2015-01-02 10:21, Cameron Simpson wrote: > On 02Jan2015 10:00, Ervin Hegedüs wrote: >>On Thu, Jan 01, 2015 at 05:13:31PM -0600, Anthony Papillion wrote: >>> I have a function I'm writing to delete wildcarded files in a directory. >>> I tried this: >>> >>> def unlinkFiles(): >>> os.remove("/home/anthony/backup/unix*") >>> >>> This doesn't seem to work because it's a wildcard filename. What is the >>> proper way to delete files using wildcards? >> >>Now I didn't checked, but once I've used some like this: >> >>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. It'll also match "uni". > Consider: > > if f.startswith('unix'): > > instead of using a regular expression. > > But generally the OP will probably want to use the glob module to expand the > shell pattern as suggested by others. >