Python file manipulation function