Ad

Get File Object Used By A CSV Reader/Writer Object

- 1 answer

Is there any way to access the file object used by a CSV writer/reader object after it has been instantiated? I openned up the csv module, and it appears it's contest are builtin. I also tried setting the file object as a property but I get the following error:

AttributeError: '_csv.writer' object has no attribute 'fileobj'
Ad

Answer

csv.writer is a "builtin" function. That is, it is written in compiled C code rather than Python. So its internal variables can't be accessed from Python code.

That being said, I'm not sure why you would need to inspect the csv.writer object to find out the file object. That object is specified when creating the object:

w = csv.writer(fileobj, dialect, ...)

So if you need to access that object later, just save it in another variable.

Ad
source: stackoverflow.com
Ad