Watereon
Watereon

Reputation: 3

FileNotFoundError: No such file or directory

I'm new to programming and just following the steps provided online to build an easy game by python. I'm using VSC.

Here is my code:

import pygame

pygame.init()

# size screen
screen_width = 480
screen_height = 640
screen = pygame.display.set_mode((screen_width, screen_height))

# title 
pygame.display.set_caption("Practice")

#load strite
character = pygame.image.load(('character.png'))
character_size = character.get_rect().size
character_width = character_size[0]
character_height = character_size[1]
character_x_pos = screen_width / 2 - character_width
character_y_pos = screen_height - character_height

# event loop
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: 
            running = False
    screen.fill((117,217,242))
    screen.blit(character, (character_x_pos, character_y_pos))
    pygame.display.update()

# end game
pygame.quit()

And I got the output:

pygame 2.0.0.dev10 (SDL 2.0.12, python 3.8.3)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "/Users/suyeon/Documents/pythonWorkspace/pygame_basic/3_main_strite.py", line 15, in <module>
    character = pygame.image.load(('character.png'))
FileNotFoundError: No such file or directory.

I've tried character =

pygame.image.load(('C:/Users/suyeon/Documents/pythonWorkspace/pygame_basic/character.png'))

pygame.image.load(("C:/Users/suyeon/Documents/pythonWorkspace/pygame_basic/character.png"))

pygame.image.load(('C:\\Users\\suyeon\\Documents\\pythonWorkspace\\pygame_basic\\character.png'))

But all of them made the same error.

character.png is in the same folder.

Upvotes: 0

Views: 15050

Answers (3)

Vishal Joshi
Vishal Joshi

Reputation: 11

I think your terminal is not working on the same directory. Make your python directory and terminal directory same

Upvotes: 1

MindOfMetalAndWheels
MindOfMetalAndWheels

Reputation: 339

Regardless of the path, from the docs I don't think you should pass the path in double brackets.

pygame.image.load(r'C:/Users/suyeon/Documents/pythonWorkspace/pygame_basic/character.png')

I would check that you can access the file using the os module, you can also pass a file object, instead of a path as well as well.

Upvotes: 1

Lalit Vavdara
Lalit Vavdara

Reputation: 504

maybe you should try using pygame.image.load((r'C:/Users/suyeon/Documents/pythonWorkspace/pygame_basic/character.png'))

while specifying the path.

Upvotes: 0

Related Questions