Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete pdf files in folders and subfolders with python?

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)?

like image 286
newGIS Avatar asked Feb 08 '15 07:02

newGIS


People also ask

How do I delete multiple files in a directory in Python?

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.


2 Answers

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))
like image 109
falsetru Avatar answered Oct 13 '22 01:10

falsetru


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))
like image 31
styvane Avatar answered Oct 13 '22 03:10

styvane