This is a program for finding all .py files in a directory and zipping them into a single folder. We can substitute any file extension (.doc, .xlsx, .dxf, etc.) that we’d like to search for, but in our example we’ll use .py
To find all of the files in a given directory, we’ll import and use functionality from the FolderHierarchy script. Be sure to save this file in the same directory as the script below.
Next, we’ll filter our FolderHierarchy results to include just the .py files. Then we simply loop through the filtered DataFrame and add each file to a zip folder using the zipfile module.
# -*- coding: utf-8 -*- """ Purpose: Copy all files of a given filetype within a folder, zip them, and save. Input: 1. A folderpath to search in. 2. File type/File Extension to search for. Output: A zip file containing all of the files with the specified file extension that were folder in the specified input folder. """ import pandas as pd import zipfile import FolderHierarchy #Define what filetype to search for extension=".py" #Execute 'FolderHierarchy' program (in same directory) FolderHierarchy results=FolderHierarchy.all_levels #Filtering for just files with the defined file extension found_files=pd.concat([results[level][results[level]['Path'].str.contains(extension, regex=False)] for level in results]) #Copy and zip all of the files found. new_zip=zipfile.ZipFile('all_'+extension+ '_files'+'.zip',mode='w') #Writing to zip (https://pymotw.com/2/zipfile/) for file in found_files['Path']: new_zip.write(file,arcname=file.split('\\')[-1]) new_zip.close() print('Found '+str(len(found_files))+' '+extension+' files.')