Python File Methods

Python has a set of methods that can be used with file objects.

Method Description
close() Close the file.
detach() Return a separated raw stream (raw stream) from the buffer.
fileno() Return the numeric representation of the stream from the perspective of the operating system.
flush() Flush the internal buffer.
isatty() Return whether the file stream is interactive.
read() Return the content of the file.
readable() Return whether the file stream can be read.
readline() Return a single line from the file.
readlines() Return the list of lines in the file.
seek() Change the file position.
seekable() Return whether the file allows us to change the file position.
tell() Return the current file position.
truncate() Adjust the file to the specified size.
writeable() Return whether it is possible to write to the file.
write() Write the specified string to a file.
writelines() Write a string list to a file.

Please visit our Python File Handling Tutorial Learn more about file objects.