检查turbogears公共文件是否存在

时间:2012-08-15 16:45:09

标签: path turbogears

我是turbogears的新手,并且有一个应用程序,我正在公共/媒体/选票下创建多个目录。我需要查看目录是否存在。

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"

我尝试过多种方式来定义路径:“/ media / ballots /”,“。/ media / ballots /”,“media / ballots”。但是os.path.exists(path)总是返回false。我不确定TG如何访问公共文件。

2 个答案:

答案 0 :(得分:7)

tg.config ['paths'] ['static_files'] 将为您提供公共目录的绝对路径。然后,您可以使用 os.path.join 将其链接到文件相对于公共文件的路径。

答案 1 :(得分:0)

我必须制作路径=“projectname / public / media / ballots /”