How to access the files present in the MEDIA_ROOT in Django

251 views Asked by At

I have a file named "user_001.txt" in the MEDIA_ROOT directory. I am trying to get the file name from a View and print it on the console.

1

There are 1 answers

0
Amélie Krejčí On BEST ANSWER

Hello and welcome to StackOverflow Naga Satya!

Firstly, let me kindly suggest you take a look at StackOverflow’s “How to write a good question” page. It is always nice to write “I need to achieve X. I have tried A and B but only got result Y from that.” and add some code you have tried to use.

Django’s MEDIA_ROOT setting only configures where files uploaded to a FileField go. If you need to print a file to console, you need its path relative to where you launch manage.py from. After that it is just standard Python file reading. Check out this GeeksforGeeks tutorial about reading files.