简体   繁体   中英

How can Python get the path to a directory of images when packaging a script?

Here is my script:

current_directory=os.path.abspath(os.curdir)
self.image_array = glob.glob(os.path.join(current_directory,"./data/world_flag_game/flags/*.png")

This works fine when I run it (using quickly) from the terminal but when I package it (using quickly package) it seems that the path is not working.

If the path is not working, it probably isn't the path where the .png images are located. Try printing the path from the terminal as well as from your packaged application. Something like the following will help you to see why the images are not found.

print("%r" % current_directory)

It's not surprising that the application's build directory would be different than its development directory. So you may want to replace your os.path.join(...) with an absolute directory to verify that your application works when packaged. Then it's a matter of storing your images into the build directory and then pointing to that location correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM