Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'XmlItemExporter' object has no attribute 'file' #3398

Closed
vidarlee opened this issue Aug 22, 2018 · 2 comments · Fixed by #4022
Closed

AttributeError: 'XmlItemExporter' object has no attribute 'file' #3398

vidarlee opened this issue Aug 22, 2018 · 2 comments · Fixed by #4022

Comments

@vidarlee
Copy link

The example in the document Item Exporters
called the file attribute of XmlItemExporter instance as follow:

    def close_spider(self, spider):
        for exporter in self.year_to_exporter.values():
            exporter.finish_exporting()
            exporter.file.close()

I checked the source of scrapy/scrapy/exporters.py,
actually, there is no attribute named 'file' in XmlItemExporter.
So, when i run the script the error occurred:
"AttributeError: 'XmlItemExporter' object has no attribute 'file'".

Also, there is no 'file' attribute in CsvItemExporter while exists in JsonItemExporter,
PickleItemExporter, MarshalItemExporter , PprintItemExporter.

I think it is necessary to and a file descriptor attribute into XmlItemExporter and CsvItemExporter for consistency. Also the file descriptor should be closed when exporting has been finished.

If the file descriptor attribute will add to XmlItemExporter and CsvItemExporter, then i have another question:
Is it a good idea to add the self.file.close() into the function finish_exporting()?
It seems it is simple for user just call function finish_exporting() and won't care about the file descriptor whether closed.

@wRAR
Copy link
Member

wRAR commented Aug 27, 2018

This code was added in #2989 by @colinmorris.

I don't think anything should be done with existing exporters, as the file attribute is not documented and should not be accessed. The exporters use a file-like object opened by the outside code, so the same code should keep a reference and close it when needed. The reference is kept by some exporter classes because they need to access it, and other classes like XmlItemExporter and CsvItemExporter don't keep it because they keep a wrapper around it.

And I don't think an exporter should close a file-like object that was passed to it. This is not even possible in all cases, as currently we require only the write method to be present on the file-like object.

@Gallaecio
Copy link
Member

This is at least a documentation bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants