file write updated to work with directories