Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #83112 > unrolled thread

Re: How do I remove/unlink wildcarded files

Started byMRAB <python@mrabarnett.plus.com>
First post2015-01-02 11:29 +0000
Last post2015-01-02 11:29 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: How do I remove/unlink wildcarded files MRAB <python@mrabarnett.plus.com> - 2015-01-02 11:29 +0000

#83112 — Re: How do I remove/unlink wildcarded files

FromMRAB <python@mrabarnett.plus.com>
Date2015-01-02 11:29 +0000
SubjectRe: How do I remove/unlink wildcarded files
Message-ID<mailman.17319.1420198157.18130.python-list@python.org>
On 2015-01-02 10:21, Cameron Simpson wrote:
> On 02Jan2015 10:00, Ervin Hegedüs <airween@gmail.com> 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.
>

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web