Python Forum
Delete multiple files using file extention
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Delete multiple files using file extention
#3
Can write own function with itertools chain() to make glob take unlimited arguments.
from glob import iglob
from itertools import chain
import os

def multi_glob(*args):
    return chain.from_iterable(iglob(pattern) for pattern in args)

for filename in multi_glob('*.txt', '*.bmp', '*jpg'):
         print(filename)
         #os.remove(filename)
Quote:The python equivalent to the Linux command line :
rm *.log *.err
So can make the same i call it py_rm,using my favorite command line tool Click.
# py_rm.py
from glob import iglob
from itertools import chain
import os
import click

def iter_glob(*args):
    return chain.from_iterable(iglob(pattern) for pattern in args)

@click.command()
@click.argument('arg', nargs=-1)
def rm(arg):
    for filename in iter_glob(*arg):
         click.echo(filename)
         #os.remove(filename)

if __name__ == '__main__':
    rm()
Test from command line.
Output:
C:\code\mod λ py_rm.py *.txt *.bmp *.jpg bar.txt spam.bmp test.bmp egg.jpg
Reply


Messages In This Thread
RE: Delete multiple files using file extention - by snippsat - Jan-23-2019, 03:05 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python convert multiple files to multiple lists MCL169 6 1,628 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  splitting file into multiple files by searching for string AlphaInc 2 936 Jul-01-2023, 10:35 PM
Last Post: Pedroski55
  Merging multiple csv files with same X,Y,Z in each Auz_Pete 3 1,224 Feb-21-2023, 04:21 AM
Last Post: Auz_Pete
  unittest generates multiple files for each of my test case, how do I change to 1 file zsousa 0 985 Feb-15-2023, 05:34 PM
Last Post: zsousa
  Find duplicate files in multiple directories Pavel_47 9 3,210 Dec-27-2022, 04:47 PM
Last Post: deanhystad
  delete all files and subdirectory from a main folder mg24 7 1,661 Oct-28-2022, 07:55 AM
Last Post: ibreeden
  delete all files which contains word sql_Table1 mg24 2 889 Sep-15-2022, 10:05 PM
Last Post: mg24
  Delete multiple lines from txt file Lky 6 2,359 Jul-10-2022, 12:09 PM
Last Post: jefsummers
  Delete empty text files [SOLVED] AlphaInc 5 1,623 Jul-09-2022, 02:15 PM
Last Post: DeaD_EyE
  rename and add desire "_date" to end of file name before extention RolanRoll 1 1,270 Jun-13-2022, 11:16 AM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020