Sparrow
Sparrow

Reputation: 15

I need to delete csv an xlsx files simutaneously

I am extracting data into csv and xlsxI need to delete those files simultaneously

My code :

Csv_files=glob.glob(os.path.join("*.csv))
xl_files=glob.glob(os.path.join("*.xlsx))
for f in csv_files:
 os.remove(f)
for f in xl_files:
 os.remove(f)

Instead of removing seperately,i need to delete in 3/5 line of code

Upvotes: 0

Views: 110

Answers (2)

kaksi
kaksi

Reputation: 196

from glob import glob
import os
for f in glob('*.csv') + glob('*.xlsx'):
    os.remove(f)

Upvotes: 2

Francisco Miranda
Francisco Miranda

Reputation: 51

itertools has lots of utility methods for this sort of problems

import itertools
Csv_files=glob.glob(os.path.join("*.csv"))
xl_files=glob.glob(os.path.join("*.xlsx"))

for f in itertools.chain(csv_files, xl_files):
    os.remove(f)

Upvotes: 0

Related Questions