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[1])
    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?

Answers


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


Need Your Help

How to run Trac on Windows?

svn installation documentation trac

Looking for documentation (a guide/tutorial) that easily explains how to setup and run Trac with Subversion on Windows. Suggestions?

Drag & Drop Shapes on Canvas

c# wpf graphics canvas shapes

I used MouseDragElementBehavior to let user drag & drop Shapes on a Canvas:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.