在python中从zip加载音频文件时出错 [英] Error when loading audio file from zip in python

查看:68
本文介绍了在python中从zip加载音频文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个游戏,我需要从 .zip 文件中加载一些受密码保护的音频文件,但出现此错误:

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'

这是我下面的代码:

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'?

推荐答案

我在 python 3.6 上也遇到这个错误.

I also get this error on python 3.6.

我猜pygame.mixer.music.load 调用zippie 上的seek 方法,这是一个ZipExtFile.

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

从 python 3.7 ZipExtFile 对象现在有一个 seek 方法.我认为如果您升级到 python 3.7.2 或更新版本,那么您的错误应该会消失.

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.

这篇关于在python中从zip加载音频文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆