open file before chdir to avoid symlink problems