Python 在zipfile中运行文件?

Python 在zipfile中运行文件?,python,zipfile,Python,Zipfile,例如,是否可以运行zipfile中的.html或.exe?我正在使用Zipfile模块 以下是我的示例代码: import zipfile z = zipfile.ZipFile("c:\\test\\test.zip", "r") x = "" g = "" for filename in z.namelist(): #print filename y = len(filename) x = str(filename)[y - 5:] if x == ".ht

例如,是否可以运行zipfile中的.html或.exe?我正在使用Zipfile模块

以下是我的示例代码:

import zipfile

z = zipfile.ZipFile("c:\\test\\test.zip", "r")
x = ""
g = ""
for filename in z.namelist():
    #print filename
    y = len(filename)
    x = str(filename)[y - 5:]
    if x == ".html":
        g = filename
f = z.open(g)
f=z.open(g)
之后,我不知道下一步该怎么办。我尝试使用
.read()
但是它只读取html内部的内容,我需要的是让它运行或执行


或者还有其他类似的方法吗?

最好的方法是将所需文件提取到Windows temp目录并执行它。我已修改了您的原始代码以创建临时文件并执行它:

import zipfile
import shutil
import os

z = zipfile.ZipFile("c:\\test\\test.zip", "r")
x = ""
g = ""
basename = ""
for filename in z.namelist():
    print filename
    y = len(filename)
    x = str(filename)[y - 5:]
    if x == ".html":
        basename = os.path.basename(filename) #get the file name and extension from the return path
        g = filename
        print basename
        break #found what was needed, no need to run the loop again
f = z.open(g)

temp = os.path.join(os.environ['temp'], basename) #create temp file name
tempfile = open(temp, "wb")
shutil.copyfileobj(f, tempfile) #copy unzipped file to Windows 'temp' folder
tempfile.close()
f.close()
os.system(temp) #run the file

在命令行指定的zip存档中运行第一个
.html
文件:

#!/usr/bin/env python
import os
import shutil
import sys
import tempfile
import webbrowser
import zipfile
from subprocess import check_call
from threading  import Timer

with zipfile.ZipFile(sys.argv[1], 'r') as z:
    # find the first html file in the archive
    member = next(m for m in z.infolist() if m.filename.endswith('.html'))
    # create temporary directory to extract the file to
    tmpdir = tempfile.mkdtemp()
    # remove tmpdir in 5 minutes
    t = Timer(300, shutil.rmtree, args=[tmpdir], kwargs=dict(ignore_errors=True))
    t.start()
    # extract the file
    z.extract(member, path=tmpdir)
    filename = os.path.join(tmpdir, member.filename)

# run the file
if filename.endswith('.exe'):
    check_call([filename]) # run as a program; wait it to complete
else: # open document using default browser
    webbrowser.open_new_tab(filename) #NOTE: returns immediately
例子
作为
webbrowser
的替代方法,您可以在Windows上使用
os.startfile(os.path.normpath(filename))

我不确定您想要的行为,因此很难帮助您实现。从等式中取出zip文件。假设您有一个普通的“.html”文件,例如
g=“testfile.html”
。您希望从计算机中看到什么行为?我希望它在web浏览器上启动testfile.html。就像在g=“testfile.html”之后,有一段代码可以在web浏览器上打开它。(但是testfile.html在zipfile中)我建议您在问题中添加代码,以便在web浏览器中打开文件、运行可执行文件等,如果文件不在zip文件中,您可以使用这些代码。你已经知道如何做这部分了吗?我得到了在zipfile外部运行它的代码,挑战是如何运行它,如果它在zipfile内部,我希望重新创建,如果你打开zipfile并双击zipfile内部的html文件。它将在web浏览器上自动打开。这是在windows btwYou“获得了在zipfile之外运行它的代码”。好。如果将
z.open(g)
中的文件句柄
f
指定给此代码,会发生什么情况?什么不起作用?什么表现不同?如果您向我们展示此代码,将有所帮助。不要在Windows上使用
os.system()
使用
os.startfile()
<代码>子进程,
webbrowser
其他操作系统上的模块。
tempfile
os.environ['temp']
更健壮。
os.path.splitext()。我需要时间让这件事在我的脑海中沉淀下来谢谢你的教诲让我再学习一会儿。我需要时间让这件事在我的脑海中浮现(谢谢大家
T:\> open-from-zip.py file.zip