'How Django set path to files

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



Solution 1:[1]

You can use absolute path, in work.py you can define current path by

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

and then you can access the file with file name,

os.path.join(image_folder, xxxx.png)

Solution 2:[2]

The following works for me. Add this in the python.py file to perform this task with files:

os.path.join(settings.BASE_DIR,'File_Name.csv') 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Qiang Jin
Solution 2 Jeremy Caney