User Program Communication

Read from Text File in Python

Used to read a text file.

#import the io library which opens the file
import io

file = open("filename", "r") #r means read mode
file_contents =
file.close() #close file after opening it


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.

name_file = open("name.txt", "r");
name =
print ("The name in the file is {}".format(name))

< Commenting   |   Write to File >

© 2019 SyntaxDB. All Rights Reserved.