ABSP Ch 9, Practice Project #1

My solution to the Practice Project #1 in Chapter 9 of the excellent book “Automate the Boring Stuff with Python” (ABSP)

#! usr/bin/env python3
# by lorenzo - 21/09/2017
# selective copy - program that walks through a folder tree,
#                  searches for files with a certain file extensons
#                  and then copies them into a new folder

import os, sys, shutil

print("Enter file extension you want to search for (ex:.pdf):")
extension = input()
print("Enter location of the folder you want to search in:")
location = input()
print("Enter location of the folder where you want to place the files found:")
copyLoc = input()

if not os.path.exists(location):
    print("Location to search in does not exist!")
    sys.exit()

if not os.path.exists(copyLoc):
    print("Location to copy files does not exist!")
    sys.exit()

for folderName, subfolders, filenames in os.walk(location):
    for filename in filenames:
        if filename.endswith(extension):
            shutil.copy(os.path.join(folderName,filename),copyLoc)

Comments about the code are welcome.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s