Reputation: 93
What is the difference between a file object and a filename of a class? I'm slightly confused about this. My current answer to this question is: A file object is an object that can alter a file and a file name is just the name of the file that is being altered. But I don't think I have it quite right.
Upvotes: 2
Views: 3487
Reputation: 79
Variables are simply names that are bound to object references. Variables have no type. The type lives within the object itself.
Almost everything in python is an object.
When you open a file, you are creating a file object in memory. In order to prevent python from automatically garbage collecting this reference, you bind it to a variable name that stores the file objects memory address. If all you are doing is processing a file then having the file object temporarily live in memory might be desirable since it will be cleaned up after the process is run.
The file name you pass to open is simply a string used to locate the file and store its location in memory inside python.
Upvotes: 0
Reputation: 45261
A file object is an object that exposes "a file-oriented API (with methods such as read() or write()) to an underlying resource."
A file name is just a text string containing the name of the file. It is no different than any other string object.
Upvotes: 0
Reputation: 8097
There seems to be more confusion than you're aware of so let's go through them all
open
(or in python 2, file
)open
but still has the member functions read
, write
, etc. just like a real File Object.open
.Upvotes: 6