1

In a django project, I want to open a file "file.png" (from images folder) in "worker.py".

  • worker.py
  • images
    • file.png
    • otherfile.png

It works well using the terminal, but when the server call this function I get this error:

[Errno 2] No such file or directory: "images/file.png"

So my question is, where django set relative path? Where should I put file.png to get it in worker.py using open() function?

Thanks

4

1 回答 1

3

您可以使用绝对路径,在 work.py 中您可以通过以下方式定义当前路径

current_path = os.path.dirname(__file__)
image_folder = os.path.join(current_path, images)

然后您可以使用文件名访问该文件,

os.path.join(image_folder, xxxx.png)
于 2013-05-24T02:28:50.493 回答