How do I remove/unlink wildcarded files

Ben Finney ben+python at benfinney.id.au
Fri Jan 2 04:02:38 EST 2015


Anthony Papillion <anthony at cajuntechie.org> writes:

> I have a function I'm writing to delete wildcarded files in a
> directory.

Is the brute-force method (explicit, easy to understand) good enough?

    import os
    import os.path
    import glob

    paths_to_remove = glob.glob(
            os.path.join([
                os.path.expanduser("~anthony"), "backup", "unix*"
                ]))
    for path in paths_to_remove:
        os.remove(path)

-- 
 \           “In case you haven't noticed, [the USA] are now almost as |
  `\     feared and hated all over the world as the Nazis were.” —Kurt |
_o__)                                                   Vonnegut, 2004 |
Ben Finney




More information about the Python-list mailing list