Created
June 28, 2013 18:22
-
-
Save Slater-Victoroff/5886846 to your computer and use it in GitHub Desktop.
Simple grepping for files in python in a nice useful way.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
class PyGrep: | |
def __init__(self, directory): | |
self.directory = directory | |
def grab_all_files_with_ending(self, file_ending): | |
"""Will return absolute paths to all files with given file ending in self.directory""" | |
walk_results = os.walk(self.directory) | |
file_check = lambda walk: len(walk[2]) > 0 | |
ending_prelim = lambda walk: file_ending in " ".join(walk[2]) | |
relevant_results = (entry for entry in walk_results if file_check(entry) and ending_prelim(entry)) | |
return (self.grab_files_from_os_walk(result, file_ending) for result in relevant_results) | |
def grab_files_from_os_walk(self, os_walk_tuple, file_ending): | |
"""Made to interface with """ | |
format_check = lambda file_name: file_ending in file_name | |
directory, subfolders, file_paths = os_walk_tuple | |
return [os.path.join(directory, file_path) for file_path in file_paths if format_check(file_path)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment