The following functions are used to access data randomly.a)open() and ...
Accessing data randomly in Python
To access data randomly in Python, we can use the `seek()` and `tell()` functions. These functions are used in combination with file handling functions such as `open()` and `read()`.
1. `open()` and `readlines()`
The `open()` function is used to open a file and returns a file object. The `readlines()` function reads all the lines of a file and returns them as a list of strings. This combination allows us to access the data randomly, but it does not provide a way to move the file pointer to a specific position.
2. `seek()` and `read()`
The `seek()` function is used to move the file pointer to a specific position within the file. It takes two arguments: the first argument is the offset, which specifies the number of bytes to move, and the second argument is the optional whence parameter, which specifies the reference point for the offset. By using `seek()`, we can move the file pointer to any desired position before reading the data using the `read()` function.
3. `seek()` and `tell()`
The `seek()` function is used to move the file pointer to a specific position within the file, as explained in the previous point. On the other hand, the `tell()` function is used to get the current position of the file pointer. By using `seek()` to move the file pointer and `tell()` to get the current position, we can access data randomly within the file.
4. `tell()` and `read()`
The `tell()` function is used to get the current position of the file pointer, as explained in the previous point. The `read()` function is used to read a specified number of bytes from the file, starting from the current position of the file pointer obtained using `tell()`. By using `tell()` and `read()` together, we can read data from any desired position within the file.
Conclusion
The correct answer is option 'C' because the combination of `seek()` and `tell()` functions allows us to access data randomly within a file. By using `seek()` to move the file pointer and `tell()` to get the current position, we can read data from any desired position within the file.
The following functions are used to access data randomly.a)open() and ...
Concept:
Setting Offsets in a File:
The functions we've learned so far are utilized to access data sequentially from a file. However, if we wish to retrieve data at random, Python has the seek() and tell() methods.
The tell() method:
This function returns a number indicating the file object's current location in the file. The supplied position is the byte location from the start of the file to the current position of the file object.
syntax:
file_object.tell()
The seek() method:
This function is used to place a file object at a certain location within a file.
syntax:
file_object.seek(offset [, reference_point])
Here offset is the number of bytes by which the file object is to be moved. reference_point indicates the starting position of the file object.
Hence the correct answer is seek() and tell().
To make sure you are not studying endlessly, EduRev has designed Humanities/Arts study material, with Structured Courses, Videos, & Test Series. Plus get personalized analysis, doubt solving and improvement plans to achieve a great score in Humanities/Arts.