0

im learning pygame and im getting the error

 File "c:/Users/fester/Documents/pythonProjects/2021/penisRacingGame/main.py", line 20, in <module>
    penisimage = pygame.image.load("pythonProjects\2021\penisRacingGame\car.jpg")
pygame.error: Couldn't open pythonProjects1\penisRacingGame\car.jpg

the image is in the same directory as the code, theres no reason i would even need to specify the directory in my code, i only did because i used the code

print(os.getcwd())

to see where its looking, and it said it was looking in pythonProjects, and not penisRacingGame. But even then it still gives me the error. any help?

code:

import pygame
import os

print(os.getcwd())

pygame.init()

display_width = 800
display_height = 600

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('i will steal your toes, no exeptions')

clock = pygame.time.Clock()

penisimage = pygame.image.load("pythonProjects\2021\penisRacingGame\car.jpg")

def penismove(x,y):
    gameDisplay.blit(penisimage,(x,y))


x = (display_width * 0.45)
y = (display_height * 0.8)

crashed = False

while not crashed:
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True

    gameDisplay.fill(white)

    penismove(x,y)

    pygame.display.update()
    clock.tick(60)

pygame.quit()
quit()

code after pathing down to .\pythonProjects\ and making the file name a raw string:

import pygame
import os

print(os.getcwd())

pygame.init()

display_width = 800
display_height = 600

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('i will steal your toes, no exeptions')

clock = pygame.time.Clock()

penisimage = pygame.image.load(r".\pythonProjects\2021\penisRacingGame\car.jpg")

def penismove(x,y):
    gameDisplay.blit(penisimage,(x,y))


x = (display_width * 0.45)
y = (display_height * 0.8)

crashed = False

while not crashed:
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True

    gameDisplay.fill(white)

    penismove(x,y)

    pygame.display.update()
    clock.tick(60)

pygame.quit()
quit()

1 Answers1

0

"\202" is treated as a character with the hexadecimal charcode \x82 (decimal 130). You should use a raw string: r"pythonProjects\2021\penisRacingGame\car.jpg" or escape the backslashes: "pythonProjects\\2021\\penisRacingGame\\car.jpg".

DYZ
  • 55,249
  • 10
  • 64
  • 93
  • 1
    (To add `\202` is in octal, e.g. base 8) – MegaIng Sep 29 '21 at 23:09
  • thanks for the raw string tip, but it still isnt working, im still getting the same error, i pathed it down to the directory its looking but it still wont load. ill edit my question to include the new code – yeetybois69 Oct 01 '21 at 23:01
  • Please update the error message, too. – DYZ Oct 01 '21 at 23:04
  • 1
    Since you have a new problem with a new approach to the code, you should ask a new question rather than editing. While you're at it, please read https://stackoverflow.com/help/minimal-reproducible-example and try to cut down the code to the minimum needed to reproduce the problem. You should also make sure you can explain what a current working directory is, and what you think the current working directory is when you run the program, and why. – Karl Knechtel Oct 01 '21 at 23:23