#! /usr/bin/python -tt
import os
def searchFile(path1,ext1,fileName1):
pathList = []
for root, dirs, files in os.walk(path1):
for file in files:
if file.endswith(ext1):
pathList.append(os.path.join(root,file))
print "-----The file is present under the below path------\n"
for ele in pathList:
if fileName1 in ele:
print ele
def main():
path = raw_input("Please enter the path you wish to spider. Also make sure that the files/subfolders have the correct permissions.\n")
ext = raw_input("Enter the extension you wish to search/ find. Eg: For class files enter .class / For text file enter .txt \n")
fileName = raw_input("Enter the filename without extension. Eg For example.class, input only 'example'\n")
searchFile(path,ext,fileName)
if __name__ == '__main__':
main()
With normal files/ subfolders, it gets the path/ filename correctly however when spidering through the 'jars', python script doesn't return anything. How can I make the above script scan through the Jars ?
Jars are similar to Zip archives. To scan through jar files you can use the Python module zipfile to get its contents list or you can even read the contents. You can get the list of contents in jar using Zipfile.namelist() method, then use this list to check whether the file you are searching for is present or not.
Here is a sample code which gets the list of files present in a jar.
import zipfile
archive = zipfile.ZipFile('<path to jar file>/test.jar', 'r')
list = archive.namelist()
if you will run this in comaand line or terminal you will get output like:
['file1.class', 'file2.class' ]
where file1 and file2 are the two .class files which I had in my jar file.
Filename: searchForFiles.py
import os, zipfile, glob, sys
def main():
searchFile = sys.argv[1] #class file to search for, sent from batch file below (optional, see batch file code in second code section)
listOfFilesInJar = []
for file in glob.glob("*.jar"):
archive = zipfile.ZipFile(file, 'r')
for x in archive.namelist():
if str(searchFile) in str(x):
listOfFilesInJar.append(file)
for something in listOfFilesInJar:
print("location of "+str(searchFile)+": ",something)
if __name__ == "__main__":
sys.exit(main())
You can easily run this by making a .bat file with the following text (replace "AddWorkflows.class" with the file you are searching for):
(File: CallSearchForFiles.bat)
@echo off
python -B -c "import searchForFiles;x=searchForFiles.main();" AddWorkflows.class
pause
You can double-click CallSearchForFiles.bat to easily run it.

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With