tags:

views:

381

answers:

2

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'
+3  A: 

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.

Dan
I'm not creating the writer, a factory function is creating the csv.writer object so I never actually have a reference to the file object.
Mark Roddy
What is that factory function? Is it written in Python? Can you inspect its code to figure out how to get the file object?
Dan
A: 

From what I can tell, there is no straightforward way to get the file object back out once you put it into a csv object. My approach would probably be to subclass the csv writer and readers you're using so they can carry that data around with them. Of course, this assumes the ability to be able to directly access the types of classes the factory function makes (among other things).

B N