thelonelytree thelonelytree - 1 year ago 92
Python Question

How to search the entire HDD for all pdf files?

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.

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

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

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.

Would love to have some kind of insight on where to go from here.

To sum it up,

  1. Search the root for all pdf files.

  2. Move those files into a specific location. Lets say 'G:\Books'

Thanks in advance.

Answer Source

Your find_all function is very close to the final result. When you loop through the files, you can check their extension with os.path.splitext, and if they are .pdf files you can move them with shutil.move

Here's an example that walks the tree of a source directory, checks the extension of every file and, in case of match, moves the files to a destination directory:

import os
import shutil

def move_all_ext(extension, source_root, dest_dir):
    # Recursively walk source_root
    for (dirpath, dirnames, filenames) in os.walk(source_root):
        # Loop through the files in current dirpath
        for filename in filenames:
            # Check file extension
            if os.path.splitext(filename)[-1] == extension:
                # Move file
                shutil.move(os.path.join(dirpath, filename), os.path.join(dest_dir, filename))

# Move all pdf files from C:\ to G:\Books
move_all_ext(".pdf", "C:\\", "G:\\Books")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download