Python 3: ResourceWarning: unclosed file <_io.TextIOWrapper name='PATH_OF_FILE' Python 3: ResourceWarning: unclosed file <_io.TextIOWrapper name='PATH_OF_FILE' python-3.x python-3.x

Python 3: ResourceWarning: unclosed file <_io.TextIOWrapper name='PATH_OF_FILE'


From Python unclosed resource: is it safe to delete the file?

This ResourceWarning means that you opened a file, used it, but then forgot to close the file. Python closes it for you when it notices that the file object is dead, but this only occurs after some unknown time has elapsed.

def read_data_from_file(input_file):    current_dir = os.path.realpath(        os.path.join(os.getcwd(), os.path.dirname(__file__)))    file_full_path = current_dir+input_file    with open(file_full_path, 'r') as f:        data = f.read()    return data