har*_*eyD 3 python scripting jar ipython python-2.7
#! /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()
Run Code Online (Sandbox Code Playgroud)
对于普通文件/子文件夹,它可以正确获取路径/文件名,但是当遍历“jar”时,python 脚本不会返回任何内容。如何使上述脚本扫描 Jars ?
小智 6
Jars 类似于 Zip 档案。要扫描 jar 文件,您可以使用 Python 模块zipfile获取其内容列表,甚至可以读取内容。您可以使用方法获取jar中的内容列表Zipfile.namelist(),然后使用该列表来检查您正在搜索的文件是否存在。
这是一个示例代码,它获取 jar 中存在的文件列表。
import zipfile
archive = zipfile.ZipFile('<path to jar file>/test.jar', 'r')
list = archive.namelist()
Run Code Online (Sandbox Code Playgroud)
如果您在 comaand 行或终端中运行它,您将得到如下输出:
['file1.class', 'file2.class' ]
Run Code Online (Sandbox Code Playgroud)
其中 file1 和 file2 是我的 jar 文件中的两个 .class 文件。