How do I delete files from the directory using python

Viewed 392

want to delete pdf files from the directory using python. Having "pdffiles" name folder in that lost of pdf are there So I want to delete all files from it but don't want to delete folder, want to delete just file from folder. how can I do it.(may be using os.remove())

1 Answers

Try listdir+remove:

import os
for i in os.listdir('directory path'):
    if i.endswith('.pdf'):
        os.remove(i)
Related