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 に答える