1

I have a program that cycles through directories, mutating a dictionary with keys of the file name, in this case 'black_arrow', but when i test it, i get this obscure error:

    Traceback (most recent call last):
  File "C:\Users\Michael\Desktop\game\dicts.py", line 17, in <module>
    print(GetPics())
  File "C:\Users\Michael\Desktop\game\dicts.py", line 13, in GetPics
    load_attacks(dic)
  File "C:\Users\Michael\Desktop\game\dicts.py", line 12, in load_attacks
    dictionary [img[:-4]]=pg.image.load(img)
pygame.error: Couldn't open black_arrow.png

i tried this through the python shell, and it worked perfectly. i also tried calling pygame.init() which i did not think would do anything, and it did not. this is the main code:

def GetPics(dic={}):
    '''
    return a dictionary whose keys are the filename, and values  are
    he image itself.
    '''

    import pygame as pg, os

    try:os.chdir('data')
    except FileNotFoundError:
        pass

    def load_attacks(dictionary):
        '''
        adds keys for attacks to dictionary.
        '''
        Cpath='Attacks\\'
        DirList=os.listdir(Cpath)
        for Dir in DirList:
            for img in os.listdir(Cpath+Dir):
                dictionary [img[:-4]]=pg.image.load(img)
    load_attacks(dic) 

if __name__ == '__main__':
    a={}
    GetPics(a)
    print(a)

(put in one file)

Michael
  • 436
  • 3
  • 11
  • You are listing the images in the directory `Cpath+Dir` - but then trying to load them from the current directory, which is unlikely to be the same location. One solution would be to use `os.path.join()` to build up the full name of each image file. – jasonharper Oct 18 '17 at 02:57
  • THANK YOU! i am a complete idiot. i was completely baffled and i realize what a dumb mistake that was!!! thank you. – Michael Oct 18 '17 at 03:32

0 Answers0