Section 9.4. File Built-in Attributes


9.4. File Built-in Attributes

File objects also have data attributes in addition to methods. These attributes hold auxiliary data related to the file object they belong to, such as the file name (file.name), the mode with which the file was opened (file.mode), whether the file is closed (file.closed), and a flag indicating whether an additional space character needs to be displayed before successive data items when using the print statement (file.softspace). Table 9.4 lists these attributes along with a brief description of each.

Table 9.4. Attributes for File Objects

File Object Attribute

Description

file.closed

TRue if file is closed and False otherwise

file.encoding[a]

Encoding that this file useswhen Unicode strings are written to file, they will be converted to byte strings using file.encoding; a value of None indicates that the system default encoding for converting Unicode strings should be used

file.mode

Access mode with which file was opened

file.name

Name of file

file.newlines[a].

None if no line separators have been read, a string consisting of one type of line separator, or a tuple containing all types of line termination characters read so far

file.softspace

0 if space explicitly required with print, 1 otherwise; rarely used by the programmergenerally for internal use only


[a] New in Python 2.3.



Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net