'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
Solution 1:[1]
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)
Solution 2:[2]
from glob import glob
import os
for f in glob('*.csv') + glob('*.xlsx'):
os.remove(f)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Francisco Miranda |
| Solution 2 | kaksi |
