Issue
#! /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 ?
Solution
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.
Answered By - bonney
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.