Last active
March 13, 2019 23:05
-
-
Save hasanparasteh/64a61f3933509d322caa435e6c62f7ac to your computer and use it in GitHub Desktop.
It's grabs file extentions you want to find and copy all founded data to a folder
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 fnmatch | |
import os | |
# pip install --user psutil | |
import psutil | |
from shutil import copy | |
def get_all_hard_disk_partitions(): | |
partitions_list = psutil.disk_partitions() | |
partitions = [dp.device for dp in partitions_list if dp.fstype == 'NTFS'] | |
return partitions | |
class FindAndCopy: | |
def __init__(self, extensions, partition, directory_name): | |
self.extensions = extensions | |
self.partition = partition | |
self.directory_name = directory_name | |
self.matches = [] | |
def run(self): | |
self.create_directory() | |
for drive in self.partition: | |
self.search_file_extension(drive) | |
print("Copying Files...") | |
self.copy_to_path() | |
def search_file_extension(self, letter): | |
print("Search Started!\n") | |
for root, directoryName, fileName in os.walk(letter): | |
for extension in self.extensions: | |
for fileName in fnmatch.filter(fileName, extension): | |
self.matches.append(os.path.join(root, fileName)) | |
print("Search Finished\n") | |
return self.matches | |
def copy_to_path(self): | |
for file in self.matches: | |
try: | |
os.stat(directory) | |
copy(file, self.directory_name) | |
except IOError as e: | |
print("Unable to copy file. %s" % e) | |
def create_directory(self): | |
if not os.path.exists(self.directory_name): | |
os.makedirs(self.directory_name) | |
print("Directory Created!\n") | |
if __name__ == '__main__': | |
# Enter your data here | |
file_extensions = ['*.json', '*.txt'] | |
directory = "D:\\Founded_Files" | |
partitions_letter = get_all_hard_disk_partitions() | |
data_copy = FindAndCopy(file_extensions, partitions_letter, directory) | |
data_copy.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment