Path 检查turbogears公共文件是否存在

Path 检查turbogears公共文件是否存在,path,turbogears,Path,Turbogears,我是turbogears的新手,有一个我正在创建的应用程序,在public/media/ballots下有多个目录。我需要看看目录是否存在 path = "public/media/ballots/" + x['directoryName'] #return path if os.path.exists(path): listing = os.listdir(path) i=0 for infile

我是turbogears的新手,有一个我正在创建的应用程序,在public/media/ballots下有多个目录。我需要看看目录是否存在

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是如何访问公共文件的。

我必须设置path=“projectname/public/media/ballots/”

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

实际上,这不起作用,因为它取决于从项目目录本身内部运行项目。这在部署时通常是不正确的。这就是问题的确切答案。不要信任相对路径。