문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top