Skip to content Skip to sidebar Skip to footer

Python Directory List Returned To Django Template

Total Python newb here. I have a images directory and I need to return the names and urls of those files to a django template that I can loop through for links. I know it will be

Solution 1:

If your images are in one directory

import os
root="/my"
Path=os.path.join(root,"path","images")
os.chdir(Path)
for files inos.listdir("."):
    if files[-3:].lower() in ["gif","png","jpg","bmp"] :
         print"image file: ",files

Solution 2:

If it's a single directory, os.listdir('thedirectory') will give you a list of filename strings that seem to be suitable for your purposes (though it won't make "the urls" -- not sure how you want to make them?). If you need a whole tree of directories (recursively including subdirectories) then it's worth debugging your failed attempts at using os.walk, but it's hard for us to spot the bugs in code that we're not shown;-).

Post a Comment for "Python Directory List Returned To Django Template"