Issue
How can I list all file of a directory in Python and remove them by .log?
for root, dirs, files in os.walk(r'/home/m110/public_html/ts/'):
print(files)
output1281.log
output1020.log
log3.log
mem.py
output1087.log
bunker3.py
output1248.log
output1311.log
output1099.log
output1261.log
output1282.log
output1213.log
log43.log
output1291.log
output1095.log
output1209.log
usr.txt
output1210.log
log7.log
output1042.log
attack.py
output1032.log
output1318.log
i run this code in Centos7 but i just need list file with .log file extension for delete times
Solution
import os
import shutil
files= next(os.walk(r'/home/m110/public_html/ts/'))[2]
for filename in files:
ex=filename.split(".")[-1]
if ex=='log':
#print ex
os.remove(filename)
Answered By - sirmagid Answer Checked By - Candace Johnson (WPSolving Volunteer)