User Program Communication
Read from Text File in Python
Used to read a text file.
Syntax
#import the io library which opens the file
import io
file = open("filename", "r") #r means read mode
file_contents = file.read()
file.close() #close file after opening it
Notes
The readLines() function reads the file line-by-line, and returns an array which contains the different lines in the file.
The read function can take in an integer, which limits the number of characters returned.
Example
name_file = open("name.txt", "r");
name = name_file.read()
print ("The name in the file is {}".format(name))
name_file.close()