Computer Science

Write the use and syntax for the following methods:

  1. open()
  2. read()
  3. seek()
  4. dump()

Python File Handling

7 Likes

Answer

  1. open() — The open() method opens the given file in the given mode and associates it with a file handle. Its syntax is file_object = open(file_name, access_mode).

  2. read() — The read() method is used to read data from a file object. It reads at most 'n' bytes from the file, where 'n' is an optional parameter. If no 'n' is specified, the read() method reads the entire contents of the file. Its syntax is: file_object.read(n).

  3. seek() — This method is used to position the file object at a particular position in a file. Its syntax is: file_object.seek(offset [, reference_point]).

  4. dump() — This method is used to convert (pickling) Python objects for writing data in a binary file. Its syntax is : dump(data_object, file_object).

Answered By

3 Likes


Related Questions