Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6877847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:39:00+00:00 2026-05-27T04:39:00+00:00

I have a small problem in my code. I have the following code: import

  • 0

I have a small problem in my code. I have the following code:

import os,pygame
class npc:
    ntype = 0
    image = None
    x = 0
    y = 0
    text = ""
    name = ""
    def Draw(self,screen):
        screen.blit(self.image, [self.x*32,self.y*32])
    def __init__(self,name,nx,ny):
        f = open(name)
        z = 0
        for line in f:
            if z == 0:
                name = line
            if z == 1:
                ntype = line
            if z == 2:
                text = line
            if z == 3:
                self.image = pygame.image.load(os.path.join('img', line))
            self.x = nx
            self.y = ny
            z=z+1

The file which I am loading from is in the following format:

The Shadow
0
Hello. I am evil.
shadow.png

It is the last line which has the problem. When I try to load that png using pygame.image.load I get an error saying that it cant load that image. If I change the pygame load code to self.image = pygame.image.load(os.path.join('img', "shadow.png")) however, it works perfectly. I have looked over the files several times, and I can’t find any reason for this error. Can someone see what I am doing wrong?

Traceback:

Traceback (most recent call last):
  File "./main.py", line 26, in <module>
    rmap = g.Load_Map("l1.txt",char)
  File "/home/josiah/python/rpg/generate.py", line 31, in Load_Map
    npcs.append(npc.npc(str.split(bx,',')[1],x,y))
  File "/home/josiah/python/rpg/npc.py", line 23, in __init__
    self.image = pygame.image.load(os.path.join('img', line))
pygame.error: Couldn't open img/shadow.png
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:39:01+00:00Added an answer on May 27, 2026 at 4:39 am

    You might have a trailing newline character. Try stripping the line:

    self.image = pygame.image.load(os.path.join('img', line.strip()))
    

    Better yet, load your file differently. Instead of the loop, you could do something like this (assuming every file is formatted identically, and has at least the same number of lines):

    name, ntype, text, filename = [l.strip() for l in open(name).readlines()[:4]]
    
    # Now use the variables normally, for example:
    self.image = pygame.image.load(os.path.join('img', filename))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with the following code in an ASPX page: <script type=text/javascript>
I have a small problem with the following code. The code works fine if
I have a small problem with interfaces. Here it is in Pseudo code :
The problem can be found in the following code: def debug[T](format: String, arg1:T, arg2:Any,
I have a small question about using jquery. I have the following code: <html>
I have a small question about threading in Windows. I have the following code
I have a small problem. I drew an image using paintComponent(Graphics g) , and
I've got a small problem with the following code: $('#body').keyup(function(){ var i = $('#body').val();
I have a problem with the MASM32 assembler The following code is a Hello
have small problem, and would very much appreciate help :) I should convert byte

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.