Pygame images won't load anymore
Question: Since I started this I've had char.png in the same folder as my .py file
**not** a subfolder and it would load my images but when I added in the
ability to move left and right, now I get an error.

    
    
    Traceback (most recent call last):File "C:\Users\Shiloh\Google Drive\Python\Platformer\main.py", line 25, in <module>
        characterSprite = pygame.image.load('char.png')
    pygame.error: Couldn't open char.png
    

I've gone back, removed everything I added since it was working and it still
won't load the images anymore. I have not moved any files, I've just simply
updated them. I've searched and tried the answers previously given on here but
none of them have seemed to work in my sitation. Thanks in advance.

    
    
    import pygame
    pygame.init()
    
    #Window Resolution
    display_width = 800
    display_height = 450
    gameResolution = pygame.display.set_mode( (display_width,display_height) )
    
    #Window Title
    pygame.display.set_caption('Bubble Bobble')
    
    #Colors
    colorRed = (255,0,0)
    colorOrange = (255,160,96)
    colorYellow = (255,216,0)
    colorGreen = (0,255,0)
    colorBlue = (0,0,255)
    colorPurple = (235,193,255)
    colorBlack = (0,0,0)
    colorWhite = (255,255,255)
    
    gameClock = pygame.time.Clock()
    characterSprite = pygame.image.load('char.png')
    def sprite(x,y):
            gameResolution.blit(characterSprite,(x,y))
    x = (display_width * 0.5)
    y = (display_height * 0.5)
    x_change = 0
    
    
    crashed = False
    while not crashed:
            for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                            crashed = True
                            if event.type == pygame.KEYDOWN:
                                    if event.key == pygame.K_LEFT:
                                            x_change = -5
                                    elif event.key == pygame.K_RIGHT:
                                            x_change = 5
                            if event.type == pygame.KEYUP:
                                    if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                                            x_change = 0
            x += x_change
                    #print(event) #Prints to console
            gameResolution.fill(colorWhite)
            sprite(x,y)
            pygame.display.update() #You can also use pygame.display.flip()
            gameClock.tick(60) #Sets the value of the FPS
    
    pygame.quit()
    quit()
    


Answer: So I think with python 3.x, for `__file__` you have to wrap that in quotes. so

    
    
    mydir = os.path.dirname('__file__')
    pygame.image.load(os.path.join(mydir, 'char.png'))
    

which should fix the problem.

if it doesnt, the chosen answere
[here](http://stackoverflow.com/questions/27218125/cant-open-png-file-in-
pygame) mentions the issue with permissions. If you run the program as root,
it will start in the home directory. Which is possible that that may be the
issue, and you weren't having a problem using the absolute path. So, if you
are running it as root... stop it.

You can tell if you are in the wrong current working directory by using this

`print (os.getcwd())`

or current file

`print (os.path.dirname(os.path.realpath('__file__')))`

