[Answer]-Auto close a file opened in django __init__.py

1👍

When you terminate the Django development server with Ctrl+C, this will kill the associated Python process and thus close all opened files.

To check this behavior, you can make the following test:

  1. Start a Python interpreter and open a file:

    open('all_india_pin_code.csv')

  2. In a shell, check that the file was opened by your Python process:

    $ lsof | grep all_india_pin_code.csv

    This will return you a result like:

    python 26395 user 3r REG 254,2 0 12217876 all_india_pin_code.csv

  3. Then kill the Python interpreter. For example:

    $ kill -9 26395

  4. Check again if the file is opened. This command will return no result, because the file has been closed:

    $ lsof | grep all_india_pin_code.csv

0👍

Can’t comment since my reputation is less than 50.
I want to add open the file in this way.

x = open('all_india_pin_code.csv')

And to check if your file is still open.
Just run x.closed it will return False if it is still open.
Then run x.close() to close it. Simple

Leave a comment