Use these methods on an open
. Any unwritten data in the buffer will be flushed.
No further operations will be allowed on the file unless
it is reopened with the
For buffered files, you can use this method to make sure that all data written to the file has been physically transmitted to the storage medium. Closing a file will also flush the buffers. Avoid using this method unless you really need it, as it may make your program less efficient.
A predicate that
is a terminal; “tty” is an
ancient term that originally meant
“Teletype”, but has come to mean any
terminal or simulated terminal.
Read the next
and return them as a string.
If there are fewer than
characters remaining after
your current position, you will get all remaining
characters. If you are at the end of the file, you will
get back an empty string (
The argument is optional. If omitted, you will get the entire remaining contents of the file as a string.
Read the next text line from
and return it as a string,
including the line terminator if any.
If you need to limit the maximum size of incoming lines,
pass that size limit as the optional
argument. The default is
to return a line of any size (subject to memory
limitations). If the line exceeds
, the file position will
be left pointing to the first unread character of that
Read all remaining lines from
and return them as a list of
strings, including line terminator characters if any.
Change the file's current position. The value of
value is used to change the position:
0: Set the position to
after the beginning of the file.
1: Move the current position
toward the end of the file.
2: Move the current position
relative to the end of the file.
For example, for a file
operation would set the position to 4 bytes before
the end of the file:
This method returns the current file position relative
to the beginning as a
>>> f=open('roundtable', 'w') >>> f.write('Bedevere') >>> f.tell() 8L >>> f.seek(2L) >>> f.tell() 2L
Remove any contents of
defaults to the current position.
Write the contents of string
. This operation will not add
terminator characters; if you want newlines in your
file, include them in the string
For a sequence
containing strings, write all those strings to
. No line
terminators will be added; you must provide them
explicitly if you want them.