Sign in to follow this  
RHSDC1

Pygame couldn't open a file

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Edited by raphoz

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this