check if turbogears public file exists
-
26-06-2021 - |
题
I am new to turbogears and have an app that I am creating with multiple directories under public/media/ballots. I need to see if a directory exists.
path = "public/media/ballots/" + x['directoryName']
#return path
if os.path.exists(path):
listing = os.listdir(path)
i=0
for infile in listing:
#find path for ballot1, ballot2, etc from files in directory
i +=1
Ballot = "Ballot" +str(i)
x['Ballot']= infile
return x
else:
return "false"
I've tried multiple ways of defining the path: "/media/ballots/", "./media/ballots/", "media/ballots". But the os.path.exists(path) always returns false. I'm not sure how TG is accessing the public files.
解决方案 2
I had to make the path="projectname/public/media/ballots/"
其他提示
tg.config['paths']['static_files'] will give you the absolute path of the public directory. You can then use os.path.join to chain it to the path of your files relative to the public one.
不隶属于 StackOverflow