File
Jim|123|1tom|321|3kamil|432|1
# __author__ = liukun# coding:utf-8obj = open (' file.txt ', ' r ') line_list = Obj.readlines () #[' jim|123|1\n ', ' tom|321|3\n ', ' Kamil|432|1 ']obj.closeprint (line_list)
Open (path + file name, read-write mode) read-write mode: R read-only, r+ read and Write, W new (overwrites the original file), a append, b binaries. Common patterns such as ' RB ', ' WB ', ' r+b ', and so on read-write mode are: RU or Ua Open with Universal line feed support (PEP 278) W opens in write mode, a opens in append (starting with EOF, creates a new file if necessary) r+ open in read-write mode w+ open in read-write mode (see W) A + opens in read-write mode (see a) RB Open WB in binary read mode opens in binary write mode (see W) ab opens in binary append mode (see a) rb+ opens in binary read-write mode (see r+) wb+ opens in binary read-write mode (see w+) ab+ with Binary read-write mode open (see A +)
1, use ' W ', if the file exists, first to empty, and then (re) create, 2, use ' A ' mode, all the data to be written to the end of the file, even if you use Seek () point to the other places, if the file does not exist, it will be created automatically.
F.read ([size]) size unspecified returns the entire file if the file size >2 memory is problematic. F.read () returns "" (Empty string) File.readline () returns a row of File.readline ([size] Returns a list that contains a size row, and a size that does not specify that all rows will be returned to line #通过迭代器访问f. Write ("hello\n") #如果要写入字符串以外的数据, first convert him to a string. F.tell () F:print Returns an integer that represents the position of the current file pointer (that is, the number of bits to the file header). F.seek (offset, [start position])
Used to move the file pointer offset: unit: bit, can be negative starting position: 0-File header, default value; 1-current position; 2-file Tail f.close () Close file
Python:open file Operations