Pygame couldn't open a file

Started by
5 comments, last by RHSDC1 11 years, 3 months ago

Hey, this is my first time on this board so I apologize if this is the wrong section (although I believe it is). I am new to python and I'm following a tutorial that is teaching me some basic stuff to get into 2D video game design. Currently I'm just putting an image into a window. This is the code/error.


Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Inte
Type "help", "copyright", "credits" or "license" for more informat
>>> import pygame, sys,os
>>> from pygame.locals import *
>>> pygame.init()
(6, 0)
>>> window = pygame.display.set_mode((468,60))
>>> window = pygame.display.set_mode((468,60))
>>> pygame.display.set_caption('Monkey Fever')
>>> screen = pygame.display.get_surface()
>>> monkey_head_file_name = os.path.join("data","chimp.bmp")
>>> print monkey_head_file_name
data\chimp.bmp
>>> monkey_surface = pygame.image.load(monkey_head_file_name)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
pygame.error: Couldn't open data\chimp.bmp
>>>

I am certain that I have the files and that they are in the proper directory.

Advertisement

Folder layouts please.

According to your logic the file is at /data/shimp.bmp.

What I think you meant was actually ./data/shimp.bmp, notice that dot in front.

The file is in C:\Program Files\Pygame-Docs\examples\data\

This is the tutorial: http://rene.f0o.com/mywiki/LectureThree and in it it says that it should be finding the file because of this section of code:


>>> monkey_head_file_name = os.path.join("data","chimp.bmp")

Sorry about that the link broke when I pasted.http://rene.f0o.com/mywiki/LectureThree

Since you are running your program from Python's interpreter, the default directory isn't the one that contains the "data" files. Therefore you need to alter the path in order to sucessfully load the .bmp file.


os.getcwd() 

will yield the current Python directory that you are working from.

To modifiy it, use :


os.chdir(path)

The path that you want to set should be : C:\Program Files\Pygame-Docs\examples\

If not, modifiy it accordingly.

And refer to Python's OS Interface for supplementary details.

Where are you running this program from, what directory. Keep in mind you have a choice to use a path that is relational to your current working directory, or using an absolute directory. Personally I would keep the data folder right next to the app at all times. Then like raphpoz said, use os.getcwd() to find out where you are. So you would use os.getcwd() + os.path.join("\data","chimp.bmp") to load the file.

Thank you both for the help, I've got it up and running now and I think I'll take kd7tck's advice and keep my files together.

This topic is closed to new replies.

Advertisement