2

I have a typical Pyramid web application setup. The application directory (I don't know what this directory is called in Pyramid?) contains the static, templates and ini.py files. In this directory I also created a directory called static_content that I use to store some special report templates.

In my view code I am using something like this to read the files in the sub-directories of the static_content directory:

f = open("/static_content/abc/report_template.tpt" , "r")

Then over in my init.py file I added a line:

config.add_static_view("static_content", "static_content")

I get an IO Error.....how do I fix this?

Regards, Mark Huang

Mark
  • 2,137
  • 4
  • 27
  • 42
  • 1
    Why not just let the static view serve them? I'm assuming you have a reason that you're attempting to open the files yourself? – Michael Merickel Jun 14 '12 at 03:15
  • Each company has a different report template style. And based on the person that generates the report, we can tell which company he is from. From that information, we need to open up differing report templates, don't think this can be automated right? Maybe I'm wrong. – Mark Jun 14 '12 at 03:24
  • Right but I don't understand your example. `config.add_static_view` makes a directory publicly available (publishes the content). `open` reads a file server-side. Which are you trying to do? I doubt it's both?? – Michael Merickel Jun 14 '12 at 05:58

1 Answers1

2
f = open("/static_content/abc/report_template.tpt" , "r")

A leading slash in a file's path means you're giving it the full path (the file is at this exact location). If you want a relative path, take off the leading slash:

f = open("static_content/abc/report_template.tpt" , "r")

This tells it to follow that path from the current directory.

You may want to look at this question in order to build a relative path from the script file.

Community
  • 1
  • 1
Brendan Long
  • 53,280
  • 21
  • 146
  • 188
  • 1
    I think this is more related to the way Pyramid works behind Nginx. On my local development machine, doing /static_content/...... works. But once put behind Nginx, I get a bad IO Error saying that the file/directory cannot be found. I'm thinking if this has to do with setting the root in Nginx? – Mark Jun 14 '12 at 03:06
  • 1
    @Mark - `open` is a built-in Python function. My best guess is that you `/static_content` is actually in that exact path on your development machine, but in a different place on your other one. – Brendan Long Jun 14 '12 at 03:57