Jump to content

  • Log In with Google      Sign In   
  • Create Account

Pygame couldn't open a file


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 RHSDC1   Members   -  Reputation: 102

Like
0Likes
Like

Posted 31 December 2012 - 07:00 PM

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.



Sponsor:

#2 kd7tck   Members   -  Reputation: 715

Like
0Likes
Like

Posted 31 December 2012 - 09:50 PM

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.



#3 RHSDC1   Members   -  Reputation: 102

Like
0Likes
Like

Posted 01 January 2013 - 10:09 AM

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")


#4 RHSDC1   Members   -  Reputation: 102

Like
0Likes
Like

Posted 01 January 2013 - 10:10 AM

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



#5 raphoz   Members   -  Reputation: 101

Like
0Likes
Like

Posted 01 January 2013 - 01:35 PM

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, 01 January 2013 - 01:36 PM.


#6 kd7tck   Members   -  Reputation: 715

Like
0Likes
Like

Posted 01 January 2013 - 02:05 PM

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.



#7 RHSDC1   Members   -  Reputation: 102

Like
0Likes
Like

Posted 02 January 2013 - 02:52 PM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS