Error when loading audio file from zip in python

2019-07-27 07:24发布

I am making a game, and I need to load some password protected audio files from a .zip file, but I get this error:

io.UnsupportedOperation: seek
io.UnsupportedOperation: seek
io.UnsupportedOperation: seek
b'hey you did it!' #THIS IS FROM THE PROGRAM
Traceback (most recent call last):
  File "C:\Python36\lib\zipfile.py", line 849, in read
    data = self._read1(n)
  File "C:\Python36\lib\zipfile.py", line 917, in _read1
    data += self._read2(n - len(data))
  File "C:\Python36\lib\zipfile.py", line 949, in _read2
    data = self._fileobj.read(n)
  File "C:\Python36\lib\zipfile.py", line 705, in read
    self._file.seek(self._pos)
AttributeError: 'NoneType' object has no attribute 'seek'

And this is my code below:

from zipfile import ZipFile
from PIL import Image
from io import BytesIO
import pygame
from pygame.locals import *
import pyganim
import sys

pygame.init()
root = pygame.display.set_mode((320, 240), 0, 32)
pygame.display.set_caption('image load test')


#THIS IS HOW TO LOAD IMAGES (WORKS)
with ZipFile("spam.zip", 'r') as archive:
    mcimg = archive.read('a.png', pwd=b'onlyforthedev')
    mc = pygame.image.load(BytesIO(mcimg))
    anime = pyganim.PygAnimation([(mc, 100),
                                  (mc, 100)])
    anime.play()

#THIS IS HOW TO LOAD MUSIC (DOES NOT WORK)
with ZipFile('spam.zip') as zippie:
    with zippie.open('zora.mp3', pwd=b'onlyforthedev') as zora:
        pygame.mixer.music.load(zora)
        pygame.mixer.music.play(-1)

#THIS IS HOW TO LOAD TEXT (WORKS)
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt', pwd=b'onlyforthedev') as myfile:
        print(myfile.read())

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    root.fill((100, 50, 50))
    anime.blit(root, (100, 50))
    pygame.display.update()

What can I do to load sound files without raising such an error? And what is 'seek'?

1条回答
做自己的国王
2楼-- · 2019-07-27 07:43

I also get this error on python 3.6.

I am going to guess that pygame.mixer.music.load calls the seek method on zippie, which is a ZipExtFile.

From python 3.7 ZipExtFile objects now have a seek method. I think that if you upgrade to python 3.7.2 or newer, then your error should go away.

查看更多
登录 后发表回答