I am using Google App Engine for hosting my website which is mostly static pages. But there is a part where I need to get a list of images from a folder in the server and dynamically add those images to the page using JavaScript.
I got it working in my local machine using os.walk("static/images/temp/1.jpg")
in a python script. But after deploying the site, it doesn't work. The python script return empty. Should there be a different approach for getting list of static files in Google App Engine?
The main issue is that files in a static directory (which I'm assuming you're using to serve your files) are stored in a different location than your code, and are therefore not in your code's file directory.
One thing that you can do (see here for a similar issue) is create symlinks to your files (or just copy them) in another directory that you do not declare as a static_dir
(or at all) in your app.yaml
. It will then live with your application code and you can reference it using os
. For instance, here is some very simple code that iterates through the files in a directory called upload_stuff
in the root:
class MainPage(webapp2.RequestHandler):
def get(self):
path = os.path.join(os.path.dirname(__file__), 'upload_stuff')
for filename in os.listdir(path):
# Here you can do what you need with the files
self.response.out.write(filename)
You can adjust this to work how you want, but the basic idea is that you will need to store the files with your code as well as in static form.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With