pyhook - How to find files and save them to different folder in python -
i amt trying find relevant files , folders local repository. e.g. pulled projects git local machine , want search specific files(.txt) , save them new folder in same pattern (root->dir-> file). want rid of files doesn't match name , extension keep same format. in advance.
you can traverse directory tree finding files want this:
import os, fnmatch def find_files(directory, pattern): root, dirs, files in os.walk(directory): basename in files: if fnmatch.fnmatch(basename, pattern): filename = os.path.join(root, basename) yield filename def find_files_to_list(directory, pattern): file_list = [] root, dirs, files in os.walk(directory): basename in files: if fnmatch.fnmatch(basename, pattern): filename = os.path.join(root, basename) file_list.append(filename) return file_list
then copy repository , this:
wanted_files = find_files_to_list('/original_project/', '*.html') filename in find_files('/copy_project/', '*'): if filename not in wanted_files: os.remove(filename)
Comments
Post a Comment