Question

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'
Was it helpful?

Solution

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.

OTHER TIPS

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top