0

I want to create files with the extension .txt, .csv, .bin, etc. so the ideal would be with import os I think or if there is some other better way.

But I don't want to save those in the DB they are to consult the once or sporadically, that is, locally are uploaded to the server and then it is replaced and that's it.

In a simple way, I create the files I need, upload them and replace them when I need something that will not be so common.

The directory for these local files would be /app/files/.

1 Answer 1

1

I managed to create files using directory path, something like

imports
def createFile (self, my_data, nameFile):
        directory = "./my_app/folder/" + nameFile + ".txt" # PATH directory
        my_File = open (directory, "w")
        my_File.write (my_data)
        my_File.close ()
        return true

In itself the important thing is import os and write well the directory where the file will be created. No need to modify anything else.

In the Django documentation I saw something in the import of some classes but in my case this was enough, on the other hand now that I write this answer from django.core.files import File which as seen is the equivalent of import os

I tried to use these

from django.core.files.base import ContentFile
from django.core.files.storage import default_storage
from django.core.files.storage import FileSystemStorage

As I do not have time and I had problems with these, I went with the easy

PS: maybe then use from django.core.files import File instead of import os as it seems to be the same, if so I update the answer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.