python zipfile module on windows, too many slashes
I'm currently making a game using python 2.7 and pygame 1.9. I store my graphics in graphics.dat, which is just a renamed zip file. The function I use to load graphics into the game is this:
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) if noAlpha: resultSurf = preSurf.convert() else: resultSurf = preSurf.convert_alpha() baseZip.close() return resultSurf
On linux (Ubuntu 12.04) it works perfectly, but on windows it always raises an error
"No item named folder\\\\\\\file.png in the archive"
(I'm not on Windows right now so I can't copy the exact error text, but it has a lot of "\"s).
Is there a solution?
I think you need to split filename with '\' and use only image.png from it, rather than folder\image.png. Just try and let us know if it worked. I am guessing this because I do not know the contents of the zip file as well as your current working directory
The problem is that filenames in zip files are always stored with forward slashes, never backslashes.
Thus, when loading your image, you'll have to use a path with forward slashes as path separators, e.g. 'folder/image.png'.