The built-in file
function creates a
file
object. The resulting object has a number of
operations that change the state of the file, read or write data, or
return information about the file. In the following descriptions,
f
is a file
object,
created by the file
.
Read Methods. The following methods read from a file. As data is read, the
file position is advanced from the beginning to the end of the file.
The file must be opened with a mode that includes or implies
'r'
for these methods to work.
-
f.
read
(
[size]
) → string
-
Read as many as
size
characters from
file
f
. If
size
is negative or omitted, the rest of the file is read.
-
f.
readline
(
[size]
) → string
-
Read the next line or as many as
size
characters from file
f
; an incomplete
line can be read. If
size
is negative or
omitted, the next complete line is read. If a complete line is
read, it includes the trailing newline character,
\n
. If the file is at the end,
f.
readline
returns a zero length string
. If the file
has a blank line, this will be a string
of
length 1 (the newline character).
-
f.
readlines
(
[hint]
) → list of strings
-
Read the next lines or as many lines from the next
hint
characters from file
f
. The value of
hint
may be rounded up to match an internal
buffer size. If
hint
is negative or
omitted, the rest of the file is read. All lines will include the
trailing newline character, \n
. If the file is
at the end,
f.
readline2
returns a zero length
list
.
Write Methods. The following methods writing to a file. As data is written, the
file position is advanced, possibly growing the file. If the file is
opened for write, the position begins at the beginning of the file. If
the file is opened for append, the position begins at the end of the
file. If the file does not already exist, both writing and appending
are equivalent. The file must be opened with a mode that include
'a'
or 'w'
for these methods to
work.
-
f.
flush
-
Flush all accumulated data from the internal buffers of file
f
to the OS file. Depending on your OS,
this may also force all the data to be written to the
device.
-
f.
write
(
string
)
-
Write the given string
to file
f
. Buffering may mean that the
string
does not appear on any console until
a close
or flush
operation is used.
-
f.
writelines
(
list
)
-
Write the list
of
string
s to file
f
. Buffering may mean that the
string
s do not appear on any console until
a close
or flush
operation is used.
-
f.
truncate
〈
[size]
〉
-
Truncate file
f
. If
size
is not given, the file is truncated at
the current position. If
size
is given, the
file will be truncated at
size
. If the file
isn't as large as the given
size
, the
results vary by operating system. This function is not available
on all platforms.
Position Control Methods. The current position of a file can be examined and changed.
Ordinary reads and writes will alter the position. These methods will
report the position, and allow you to change the position that will be
used for the next operation.
-
f.
seek
(
offset
,
[
whence
] )
-
Change the position from which file
f
will be processed. There are three
values for
whence
which determine the
direction of the move. If
whence
is zero
(the default), move to the absolute position given by
offset
. f.seek(0)
will rewind
file f
. If
whence
is
one, move relative to the current position by
offset
bytes. If offset is negative, move
backwards; otherwise move forward. If
whence
is two, move relative to the end of
file. f.seek(0,2)
will advance file
f
to the end, making it possible to
append to the file.
-
f.
tell
→ integer
-
Return the position from which file
f
will be processed. This is a partner
to the seek
method; any position returned by
the tell
method can be used as an argument to
the seek
method to restore the file to that
position.
Other Methods. These are additional useful methods of a file object.
-
f.
close
-
Close file
f
. The closed flag is
set. Any further operations (except a redundant close) raise an
IOError
exception.
-
f.
fileno
→ integer
-
Return the internal file descriptor (FD) used by the OS
library when working with file
f
. A
number of Python modules provide functions that use the OS
libraries; the OS libraries need the FD.
-
f.
isatty
→
boolean
-
Return True
if file
f
is connected to the console or
keyboard.
-
f.
closed
-
This attribute is True
if file
f
is closed.
-
f.
mode
-
This attribute of file
f
is the
mode argument to the file
function that was
used to create the file object.
-
f.
name
-
This attribute of file
f
is the
filename argument to the file
function that
was used to create the file object.