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
#1
I script in bash and some expect and I am trying to make the move to python.
I have taken a beginner class at a local community college and am now trying to put "it" to use.
The best way for me to do so is to look at what I have written in bash and expect and rewrite in python.


I am working at a Centos Linux VM command line
Python 2.6.6 (I know old and crusty)

So my question :
Quote:#!/usr/bin/python
import glob, os
for f in glob.glob("*.err"):
os.remove(f)

The snip of code works well to delete any file with an extension of ".err".
For instance, I want to be able to delete any file with an extension of .err and the extension of .log

The python equivalent to the Linux command line :
Quote:rm *.log *.err

Thanks in advance !!
Reply
#2
As far as i am aware i dont think there is a way to return multiple hits with glob.

use zip to iterate over both
for f1,f2 in zip(glob.glob('*.err'), glob.glob('*.log')):
    os.remove(f1)
    os.remove(f2)
or for a shorter for loop header
err = glob.glob('*.err')
log = glob.glob('*.log')

for f1,f2 in zip(err, log):
    os.remove(f1)
    os.remove(f2)
or just use a function
def remove_files(ext):
    for f in glob.glob(ext):
        os.remove(f)
        
remove_files('*.err')
remove_files('*.log')
Recommended Tutorials:
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  python convert multiple files to multiple lists MCL169 6 1,431 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  splitting file into multiple files by searching for string AlphaInc 2 812 Jul-01-2023, 10:35 PM
Last Post: Pedroski55
  Merging multiple csv files with same X,Y,Z in each Auz_Pete 3 1,084 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 918 Feb-15-2023, 05:34 PM
Last Post: zsousa
  Find duplicate files in multiple directories Pavel_47 9 2,924 Dec-27-2022, 04:47 PM
Last Post: deanhystad
  delete all files and subdirectory from a main folder mg24 7 1,526 Oct-28-2022, 07:55 AM
Last Post: ibreeden
  delete all files which contains word sql_Table1 mg24 2 821 Sep-15-2022, 10:05 PM
Last Post: mg24
  Delete multiple lines from txt file Lky 6 2,199 Jul-10-2022, 12:09 PM
Last Post: jefsummers
  Delete empty text files [SOLVED] AlphaInc 5 1,510 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,208 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