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.