f is either string or file