Demonstrates reading from a text file : Text file « File « Python Tutorial






print "Opening and closing the file."
text_file = open("read_it.txt", "r")
text_file.close()

print "\nReading characters from the file."
text_file = open("read_it.txt", "r")
print text_file.read(1)
print text_file.read(5)
text_file.close()

print "\nReading the entire file at once."
text_file = open("read_it.txt", "r")
whole_thing = text_file.read()
print whole_thing
text_file.close()

print "\nReading characters from a line."
text_file = open("read_it.txt", "r")
print text_file.readline(1)
print text_file.readline(5)
text_file.close()

print "\nReading one line at a time."
text_file = open("read_it.txt", "r")
print text_file.readline()
print text_file.readline()
print text_file.readline()
text_file.close()

print "\nReading the entire file into a list."
text_file = open("read_it.txt", "r")
lines = text_file.readlines()
print lines
print len(lines)
for line in lines:
    print line
text_file.close()

print "\nLooping through the file, line by line."
text_file = open("read_it.txt", "r")
for line in text_file:
    print line
text_file.close()

raw_input("\n\nPress the enter key to exit.")








12.3.Text file
12.3.1.First Python Programs
12.3.2.File Read and Display
12.3.3.Demonstrates reading from a text file
12.3.4.Reads a plain text file
12.3.5.Writing to a text file
12.3.6.Open a file and save text to it
12.3.7.Storing and parsing Python objects in files
12.3.8.print to a file