Author Topic: Python File Searcher.  (Read 620 times)

0 Members and 1 Guest are viewing this topic.

Offline $Clone

  • Peasant
  • *
  • Posts: 86
  • Cookies: 5
  • $---Shadowalker---$
    • View Profile
Python File Searcher.
« on: July 19, 2014, 10:45:37 pm »
I know that there are many file searchers out there...but what tha heck!... ;D .
this script searches for a file or directory and lists down the search results with a count.
Code: (Python) [Select]
#########################################################
##Windows,Mac and Linux file Searcher.            #######
##Tested on windows                               #######
##with a few changes can work in mac os and linux.#######     
##Remember this is just another python script.    #######                   
##you can add tkinter,PyQt,wx gui to it.          #######                         
##its not much but hey!...whose asking????.       #######             
#########################################################

import os
filename=""
directory=""
count=0
temp=[]
print"\t\t\t***SEARCHER SCRIPT***"
print"------------------------------------------------------------"
#Enter the file name to be searched.
filename=raw_input("Enter filename:")
#Where to search.
drive=raw_input("Enter Drive:")#C:, D:,E: if its windows... "/" if its linux.
drive=drive.capitalize()+":\\"
print"Searching..."
for rootDir,Dir,fileX in os.walk(drive):
    #Search if its a directory.
    for Dir_ in Dir:
        if filename==Dir_:
            temp.append(rootDir+"\\"+Dir_)
            count=count+1
            print "%d.)%s\%s"%(count,rootDir,Dir_)#output the file.
    #Search if its a file.       
    for File_ in fileX:
        if filename==File_:
            temp.append(rootDir+"\\"+File_)
            count=count+1
            print "%d.)%s\%s"%(count,rootDir,File_)#output the file.
print"-------------------------------------------------------"
print"-------------------------------------------------------\n"
if count==0:
    print"Sorry no file/directory found!\n"
elif count==1:
    print "%d file/directory found!" %count
else:
    print"%d files/directories found!"%count

print"End of Search!"


#os.system("pause")-->You can add this just in case the
#window is ran directly"Double clicking1"

you can add os .startfile() function to start the file if its an executable.You can add a loop to iterate over the temp list and open the file.e.g
Code: (Python) [Select]
for file in temp:
       if file.endswith(".exe"):
                 os.startfile(file)
       else:
           etc..etc...

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Python File Searcher.
« Reply #1 on: July 20, 2014, 08:48:42 am »
Not bad, let's dissect the code.

First your comment is misleading:
Code: [Select]
##Windows,Mac and Linux file Searcher.            #######
##Tested on windows                               #######
##with a few changes can work in mac os and linux.#######
It's not a Linux or Mac searcher if it needs modifications to work on these systems.

Import statement is not separated from overall code.
Print has no space after it.
You don't really need to capitalize the drive letter...
Variable names are meaningless:
Code: [Select]
for Dir_ in Dir:I have no idea what is Dir_ and what is Dir...

Overall you need to read about Python conventions here: http://legacy.python.org/dev/peps/pep-0008/

In general - not bad. Could have command line args for power users tho. +1 anyway.

Offline $Clone

  • Peasant
  • *
  • Posts: 86
  • Cookies: 5
  • $---Shadowalker---$
    • View Profile
Re: Python File Searcher.
« Reply #2 on: July 20, 2014, 12:41:38 pm »
thnx :) .... the  "for Dir_ in Dir" loop is to get the directory from the list "Dir" referenced from "os.walk() "function,meaning if the file name is a directory the "Dir_ in Dir" is where the search will get its results.In linux its already got grep,find and locate so i don't think its worth it....but if one was to build a malware this is the kind of search you will need but in c/c++.Mac i don't know realy about mac???? :-\