Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting files of specific extension using * in Python

I have couple of text files named as:

temp1.txt
temp2.txt
temp3.txt
temp4.txt
track.txt

I want to delete files only starting with temp and ending with .txt. I tried using os.remove("temp*.txt") but I'm getting error as:

The filename, directory name, or volume label syntax is incorrect: 'temp*.txt'

What is the right way to do this using python 3.7?

like image 778
Bogota Avatar asked Nov 16 '25 03:11

Bogota


1 Answers

from pathlib import Path

for filename in Path(".").glob("temp*.txt"):
    filename.unlink()
like image 171
finswimmer Avatar answered Nov 17 '25 20:11

finswimmer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!