Trevor Scott Cohen
Trevor Scott Cohen

Reputation: 93

file object Vs. a filename

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

Answers (3)

Anatzum
Anatzum

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

Rick
Rick

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

randomusername
randomusername

Reputation: 8097

There seems to be more confusion than you're aware of so let's go through them all

  • File Object: an object returned by a call to open (or in python 2, file)
  • File-like Object: an object that is not necessarily returned by open but still has the member functions read, write, etc. just like a real File Object.
  • Filename: the name of a file, usually passed as an argument to open.
  • Filename of a Class: the name of the python source file in which the class was defined.

Upvotes: 6

Related Questions