python使用7z解压apk包的方法

yipeiwu_com6年前Python基础

本文实例讲述了python使用7z解压apk包的方法。分享给大家供大家参考。具体如下:

这段代码通过shell调用7z对apk包进行解压缩

def run_shell(command, mayFreeze=False):
 def check_retcode(retcode, cmd):
 if 0 != retcode:
 print >> sys.stderr, 'err executing ' + cmd + ':', retcode
 sys.exit(retcode)
 def read_close(f):
 f.seek(0)
 d = f.read()
 f.close()
 return d
 #print >> sys.stderr, '-- Executing', command
 if mayFreeze:
 tempout, temperr = tempfile.TemporaryFile(), tempfile.TemporaryFile()
 #open(os.devnull, 'w')
 p = subprocess.Popen(command, stdout=tempout, stderr=temperr)
 p.wait()
 output, errout = read_close(tempout), read_close(temperr)
 else:
 p=subprocess.Popen(command,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
 output = p.stdout.read()
 p.wait()
 errout = p.stderr.read()
 p.stdout.close()
 p.stderr.close()
 #check_retcode(p.returncode, command)
 return (output.strip(), errout.strip())
#z7 is the full path to 7z.exe
#at times you have to encode the command into GBK/UTF8
run_shell(u'{0} -y -o"{1}" {2} x "{3}"'.format(z7, tempdir, icon, apk))
shutil.copy(u'{0}/{1}'.format(tempdir,os.path.basename(icon)),dst_path)

希望本文所述对大家的Python程序设计有所帮助。

相关文章

基于Python3 逗号代码 和 字符图网格(详谈)

逗号代码 假定有下面这样的列表: spam=['apples','bananas','tofu',' cats'] 编写一个函数,它以一个列表值作为参数,返回一个字符串。该字符串包...

Django项目中用JS实现加载子页面并传值的方法

在Django的开发过程中,有一些功能是通过JS根据用户的不同选择来加载页面中的某一部分(子页面)的。如果子页面中有我们需要传入的值。可以这么实现 在js函数中调用 $("#bas...

Python中三元表达式的几种写法介绍

要介绍Python的三元表达式,可以先看看其他编程语言比如C,JAVA中应用: public class java { public static void main(String...

浅谈Python类里的__init__方法函数,Python类的构造函数

如果某类里没有__init__方法函数,通过类名字创建的实例对象为空,切没有初始化;如果有此方法函数,通常作为类的第一个方法函数,有点像C++等语言里的构造函数。 class Ca:...

Ubuntu下Anaconda和Pycharm配置方法详解

Ubuntu下Anaconda和Pycharm配置方法详解

本文为大家分享了Ubuntu下Anaconda和Pycharm的配置方法,供大家参考,具体内容如下 1.对于Ubuntu18.04,一开始会有一个系统默认的python解释器,是3.6版...