open() and read() methods play a vital role in handling files in Python.
open():
open() method is used to read and write files.
Syntax:
open( filename, mode );
modes are r and w. r for reading and w for writing. mode is optional.
Sample Code:
fileDetails = open( 'test.txt' );
print( fileDetails );
for statement can be used to display file contents line by line. Each line from the file will will have new line. So, rstrip() method can be used to remove it.
Sample Code:
fileDetails = open( 'test.txt' );
for objFileDetail in fileDetails:
print( objFileDetail.rstrip() );
read():
read() method is used to get the file content.
Sample Code:
fileDetails = open( 'test.txt' );
objFileDetail = fileDetails.read();
print( objFileDetail );