响应式网站介绍,平面设计线上培训哪个好,哪个网站可以代做试题,赣州网上商城系统工作中经常需要提取apk文件的特定URL地址#xff0c;如是想到用Python脚本进行自动处理。需要用到的Python基础知识如下:os.walk()函数声明#xff1a;os.walk(top,topdownTrue,οnerrοrNone)(1)参数top表示需要遍历的顶级目录的路径。(2)参数topdown的默认值是“True”表示…工作中经常需要提取apk文件的特定URL地址如是想到用Python脚本进行自动处理。需要用到的Python基础知识如下:os.walk()函数声明os.walk(top,topdownTrue,οnerrοrNone)(1)参数top表示需要遍历的顶级目录的路径。(2)参数topdown的默认值是“True”表示首先返回顶级目录下的文件然后再遍历子目录中的文件。当topdown的值为False时表示先遍历子目录中的文件然后再返回顶级目录下的文件。(3)参数onerror默认值为None表示忽略文件遍历时的错误。如果不为空则提供一个自定义函数提示错误信息后继续遍历或抛出异常中止遍历。返回值函数返回一个元组含有三个元素。这三个元素分别是每次遍历的路径名、路径下子目录列表、目录下文件列表。os.walk使用实例删除某个文件夹(当然可以通过os.listdir的递归调用删除)复制代码 代码如下:#! /usr/bin/env python#codingutf-8import osdef Remove_dir(top_dir):if os.path.exists(top_dir)False:print not existsreturnif os.path.isdir(top_dir)False:print not a dirreturnfor dir_path,subpaths,files in os.walk(top_dir,False):for file in files:file_pathos.path.join(dir_path,file)print delete file:%s %file_pathos.remove(file_path)print delete dir:%s %dir_pathos.rmdir(dir_path)#调用Remove_dir(rC:\Users\Administrator\Desktop\abc)Python执行系统命令的方法 os.system()os.popen()commands.getstatusoutput()os.system()无法获得到输出和返回值通过os.popen() 返回的是 file read 的对象对其进行读取 read() 的操作可以看到执行的输出但是得不到返回值。通过 commands.getstatusoutput() 方法就可以获得到返回值和输出(status, output) commands.getstatusoutput(cat /proc/cpuinfo)3. Python中operator模块的contains(...) 函数contains(a, b) -- Same as b in a (note reversed operands). 判断b是否被a包含基础知识介绍完了可以上代码了复制代码 代码如下:import osimport operatorimport commands#from signature import *inputdir ./tmpfor path, dir, files in os.walk(inputdir):for file in files:if not file.endswith(.apk):#print not apk file.continueapkpath os.path.join(inputdir, file)cmd ./xxx -d %s %apkpathoutput os.popen(cmd)s set()#按行查找URLfor line in output:if operator.contains(line, http://):#print tmpstart line.index(http://)end line.index(,start)url line[start:end]s.add(url)cmd ./yyy -t a.expense.mdk.a.tvd %s %apkpath#获取命令执行结果及返回值status, output commands.getstatusoutput(cmd)# print outputif output.startswith(find):print outputfor url in s:if url.find(imei)!-1:print url is %s %url.strip()#print s 本文标题: Python获取apk文件URL地址实例本文地址: http://www.cppcns.com/jiaoben/python/100954.html