Uses 'with' statement when opening files to guarantee files are closed even when the process is interrupted.