Python Tutorial Python Advanced Python References Python Libraries

Python - File Handling Methods



The Python has a number of methods and functions that is used for file handling. These methods and functions are listed below in alphabetical order. For more details about any one of these, please visit its page.

File Handling Methods
MethodsDescription
close() Closes the file.
detach() Returns the separated raw stream from the buffer.
fileno() Returns the file descriptor of the file stream as a number.
flush() Flushes the internal buffer.
isatty() Returns whether the file stream is interactive or not.
open() Opens a file and returns a file object.
read() Returns the file content.
readable() Returns whether the file stream can be read or not.
readline() Returns one line from the file.
readlines() Returns a list of lines from the file.
seek() Change the file position.
seekable() Returns whether the file allows us to change the file position.
tell() Returns the current file position.
truncate() Resizes the file to a specified size.
writable() Returns whether the file can be written to or not.
write() Writes the specified string to the file.
writelines() Writes a list of strings to the file.

5