This error occurs only when you try to do some operations on a closed file. Try checking for indentation of your code or whether the file is opened or not.