Move file open check to a function.