2

だから、私は.jarこのコードを使用してファイルを解凍しようとしています:解凍せず、20/500ファイルのみで、フォルダ/画像はありません.zip。ファイル名にファイルを入力した場合も同じことが起こります。誰か提案はありますか?

import zipfile
zfilename = "PhotoVieuwer.jar"
if zipfile.is_zipfile(zfilename):
    print "%s is a valid zip file" % zfilename
else:
    print "%s is not a valid zip file" % zfilename
print '-'*40

zfile = zipfile.ZipFile( zfilename, "r" )

zfile.printdir()
print '-'*40

for info in zfile.infolist():
    fname = info.filename

    data = zfile.read(fname)


    if fname.endswith(".txt"):
        print "These are the contents of %s:" % fname
        print data


    filename = fname
    fout = open(filename, "w")
    fout.write(data)
    fout.close()
    print "New file created --> %s" % filename
    print '-'*40

しかし、それは機能しません、それはおそらく500ファイルのうちの10を解凍します誰かがこれを修正するのを手伝ってくれますか?

すでにありがとう!

私はPythonが私に言ったことを追加しようとしました、私はこれを手に入れました:おっと!次の理由により、編集を送信できませんでした。

本文は30000文字に制限されています。153562と入力しまし たが、エラーは次のとおりです。

Traceback (most recent call last):
  File "C:\Python27\uc\TeStINGGFDSqAEZ.py", line 26, in <module>
    fout = open(filename, "w")
IOError: [Errno 2] No such file or directory: 'net/minecraft/client/ClientBrandRetriever.class'

解凍されるファイル:

amw.Class
amx.Class
amz.Class
ana.Class
ane.Class
anf.Class
ang.Class
ank.Class
anm.Class
ann.Class
ano.Class
anq.Class
anr.Class
anx.Class
any.Class
anz.Class
aob.Class
aoc.Class
aod.Class
aoe.Class
4

2 に答える 2

3

このトレースバックは、知っておくべきことを示しています。

Traceback (most recent call last):
  File "C:\Python27\uc\TeStINGGFDSqAEZ.py", line 26, in <module>
    fout = open(filename, "w")
IOError: [Errno 2] No such file or directory: 'net/minecraft/client/ClientBrandRetriever.class'

エラーメッセージは、ファイルClientBrandRetriever.classが存在しないか、ディレクトリnet/minecraft/clientが存在しないことを示しています。書き込み用にファイルを開くとPythonでファイルが作成されるため、ファイルが存在しないことは問題になりません。ディレクトリが存在しない場合があります。

これが機能すると考えてください

>>> open('temp.txt', 'w') 
<open file 'temp.txt', mode 'w' at 0x015FF0D0>

しかし、これはそうではなく、取得しているものとほぼ同じトレースバックを提供します。

>>> open('bogus/temp.txt', 'w')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'bogus/temp.txt'

ディレクトリを作成すると修正されます。

>>> os.makedirs('bogus')
>>> open('bogus/temp.txt', 'w')
<open file 'bogus/temp.txt', mode 'w' at 0x01625D30>

ファイルを開く直前に、ディレクトリが存在するかどうかを確認し、必要に応じて作成する必要があります。

だからあなたの問題を解決するには、これを置き換えます

fout = open(filename, 'w')

これとともに

head, tail = os.path.split(filename) # isolate directory name
if not os.path.exists(head):         # see if it exists
    os.makedirs(head)                # if not, create it
fout = open(filename, 'w')
于 2012-08-07T17:32:57.040 に答える
1

動作する場合python -mzipfile -e PhotoVieuwer.jar destは、次のことができます。

import zipfile

with zipfile.ZipFile("PhotoVieuwer.jar") as z:
    z.extractall()
于 2012-08-07T17:36:00.883 に答える