我目前正在使用python 2.7和1.9的Pygame遊戲。我存儲我的圖形graphics.dat
,這僅僅是一個更名爲zip文件。我用它來裝載圖形轉換成遊戲中的功能是這樣的:蟒蛇壓縮文件模塊,太多的斜線
import pygame, zipfile, tarfile sys, os
from cStringIO import StringIO
from pygame.locals import *
def loadImage(filename, noAlpha=False):
baseZip = zipfile.ZipFile(os.path.join("..", "Data", "graphics.dat"))
imgData = baseZip.read(filename) #the "filename" argument is already os.path.join()'ed when I pass it to the function
imgDataIO = StringIO(imgData)
finalFileName = os.path.split(filename)
preSurf = pygame.image.load(imgDataIO, finalFileName[1])
if noAlpha:
resultSurf = preSurf.convert()
else:
resultSurf = preSurf.convert_alpha()
baseZip.close()
return resultSurf
在Linux(Ubuntu的12.04),它完美的作品,但在Windows它總是會引發錯誤
"No item named folder\\\\\\\file.png in the archive"
(我不是在Windows所以我現在不能複製確切的錯誤文本,但它有很多的「\」 S)。
有沒有解決方案?
如果您使用os.path.join應該找到它... assumming您使用的是正確的道路 –
哪一行,你在得到這個錯誤嗎?另外,請打印finalFileName的值。 用於複印Windows錯誤文本,使用重定向操作符'>'在命令行和輸出重定向到一個文件 – GodMan
的finalFileName VAR僅僅是一個可選的暗示,告訴pygame的圖像是PNG類型,對進口 – ansdor