I try to delete 300 pdf files. All the pdf files as different names, and they all spread in one big folder that divided to a lot of sub folders and sub sub folders. How can I do it with python (I work with python 2.7.8)?
The OS module in Python provides methods to interact with the Operating System in Python. The remove () method in this module is used to remove/delete a file path. Import the shutil module and pass the directory path to shutil. rmtree('path') function to delete a directory and all files contained in it.
Using shutil.rmtree
, you can delete directories recursively.
import shutil
shutil.rmtree('/path/to/directory/that/contains/pdfs')
If the directory contians other files that is not pdf
file, use following instead (which use os.walk
to traverse the directories recursively, and os.remove
/os.unlink
to remove the pdf file).
import os
for parent, dirnames, filenames in os.walk('/path/to/the/directory'):
for fn in filenames:
if fn.lower().endswith('.pdf'):
os.remove(os.path.join(parent, fn))
If all you want is delete just the pdf
file you can use the os.walk
function and fnmatch.fnmatch
function.
import os
from fnmatch import fnmatch
for dirpath, dirnames, filenames in os.walk(os.curdir):
for file in filenames:
if fnmatch(file, '*.pdf'):
os.remove(os.path.join(dirpath, file))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With