checking whether file exists before opening it