How to search the entire HDD for all pdf files?
<p>As the title suggests, I would like to get python 3.5 to search my root ('C:\')
for pdf files and then move those files to a specific folder.
This task can easily split into 2: 
1. Search my root for files with the pdf extension.
2. Move those to a specific folder.</p>

<p>Now. I know how to search for a specific file name, but not plural files that has a specific extension. </p>

<pre><code>import os

print('Welcome to the Walker Module.')
print('find(name, path) or find_all(name, path)')

def find(name, path):
for root, dirs, files in os.walk(path):
    print('Searching for files...')
    if name in files:
        return os.path.join(root, name)

def find_all(name, path):
result = []
for root, dirs, files in os.walk(path):
    print('Searching for files...')
    if name in files:
        result.append(os.path.join(root, name))
return result
</code></pre>

<p>This little program will find me either the 1st or all locations of a specific file. 
I, however, can not modify this to be able to search for pdf files due to the lack of knowledge with python and programming in general.</p>

<p>Would love to have some kind of insight on where to go from here. </p>

<p>To sum it up, </p>

<ol>
<li>Search the root for all pdf files. </li>
<li>Move those files into a specific location. Lets say 'G:\Books'</li>
</ol>

<p>Thanks in advance. </p>

<p>You can use <a href="https://docs.python.org/3/library/glob.html" rel="nofollow"><code>glob</code></a> from python 3.5 onwards. It supports a recursive search.</p>

<blockquote>
  <p>If recursive is true, the pattern â**â will match any files and zero or more directories and subdirectories. If the pattern is followed by an os.sep, only directories and subdirectories match.</p>
</blockquote>

<p>Therefore you can use it like</p>

<pre><code>import glob
from os import path
import shutil

def searchandmove(wild, srcpath, destpath):

    search = path.join(srcpath,'**', wild)

    for fpath in glob.iglob(search, recursive=True):
        print(fpath)
        dest = path.join(destpath, path.basename(fpath))
        shutil.move(fpath, dest)

searchandmove('*.pdf', 'C:\\', 'G:\\Books')
</code></pre>

<p>With a minimum of string wrangling. For large searches however such as from the root of a filesystem it can take a while, but I'm sure any approach would have this issue. </p>

<p>Tested only on linux, but should work fine on windows. Whatever you pass as <code>destpath</code> must already exist.</p>

python windows python-3.x