To open a file:
f= open(name[,mode[,bufsize]]])
This function returns a file object. The
is the file's pathname. The
name
argument, if given, is used to specify the buffer size (but
this is not usually necessary). The
bufsize
argument is a string with this syntax:mode
"access[+][b]"where
is accessr for read access (the default),
w for write access, and
a for append. The
+ flag specifies update access, and
b forces binary mode.
Methods defined on file objects include:
f.read()Read the entire file
and
return it as a string.f
f.read(n)Read the next characters from file
n.
If the file is exhausted, it returns an empty string
(f""); if fewer than
characters remain, you get all of
them.n
f.readline()Returns the next line from
,
including its line terminator, if any. Returns an
empty string when the file is
exhausted.f
f.readlines()Read all the lines from file and
return them as a list of strings, including
line termination characters.f
f.write(s)Write string to file
s.f
f.writelines(L)Write a list of strings to file
L.f
f.seek(p[,w])Change the file position.
The value of determines how
w
is used:p
0:
Set the position to
; this is the
default.p
1:
Move the position forward
bytes from its
current position.p
2:
Set the position to
bytes before the end of file.p
f.truncate([p])Remove any contents of the file past position
,
which defaults to the current position.p
f.tell()Returns the current file position.
f.flush()Flush the buffer, completing all transactions
against .f
f.isatty()Predicate: Is this file a terminal?
f.close()Close file .f