File operations - to read the file

1. The reading operation of file: read data from the file into the computer's memory

    read (): Returns a string that contains the value of the contents of the entire file

    readline (): Returns the value of the contents of the file string of the next line

 readlines (): Returns the value of the entire contents of the file list, each newline to the end of his string

Files over: 1:

f = open('temp.txt', 'r')

for line in f.readlines():

  # Line data processing

  pass

f.close()

2 file traversal:

f = open('tmp.txt', 'r')

for line in f:

  # Line data processing

  pass

f.close()

Guess you like

Origin www.cnblogs.com/sophe/p/11583686.html