So i had to for some reason develop a small utility to search a directory or drive for a particular filename. Decided to do the same in python to try and explore the os.walk() in python.
Here is how i did the same.
Starting with the Interface
So for this particular demonstration decided to have a command line interface that takes in 2 parameters
- -f --filename - This is where the filename to be searched will be provided as a string.
- -d --directory - This is where the directory where the filename is to be searched will be provided as a string.
The code for this looks pretty simple and was written as below:
1 2 3 4 5 6 7 8 | description = "Filename to search in location" parser = argparse.ArgumentParser(description=description) help_txt = "Filename with extension" parser.add_argument("-f","--filename",help=help_txt,required=True,type=str) help_txt = "Folder Name" parser.add_argument("-d","--directory",help=help_txt,required=True,type=str) args = parser.parse_args(args=sys.argv[1:]) searchfile(args.directory, args.filename) |
Here after parsing the command line argument the args.directory and the args.filename are passed to our function searchfile.
Developing the SearchFile Function
There is one additonal requirement that i will add is to copy the files that are found to a local investigate folder.
So we start by checking that the folder exists or not. If not then we go ahead and create the same.
1 2 3 4 | current_directory = os.getcwd() final_directory = os.path.join(current_directory, r'investigate') if not os.path.exists(final_directory): os.mkdir(final_directory) |
1 | x = os.walk(directory) |
(dirpath, dirnames, filenames)
We are going to first iterate through the filenames from the tuple to find whether our input filename exists or not. If it exists then we will print the full path using the dirpath and concatenating the filename to it. This will give us the full path from where we found the file.
The shutil.copy function will just copy the file in the local investigate folder.
1 2 3 4 5 | for i in x: if filename in i[2]: print(i[0] + "\\" + filename) copyfrom = os.path.join(i[0],filename) shutil.copy2(copyfrom, final_directory) |
The full code will look something like below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import sys import argparse import os import shutil def searchfile(directory,filename): current_directory = os.getcwd() final_directory = os.path.join(current_directory, r'investigate') if not os.path.exists(final_directory): os.mkdir(final_directory) x = os.walk(directory) for i in x: if filename in i[2]: print(i[0] + "\\" + filename) copyfrom = os.path.join(i[0],filename) shutil.copy2(copyfrom, final_directory) if __name__ == '__main__': description = "Filename to search in location" parser = argparse.ArgumentParser(description=description) help_txt = "Filename with extension" parser.add_argument("-f","--filename",help=help_txt,required=True,type=str) help_txt = "Folder Name" parser.add_argument("-d","--directory",help=help_txt,required=True,type=str) args = parser.parse_args(args=sys.argv[1:]) searchfile(args.directory, args.filename) |
No comments:
Post a Comment