Python:open file Operations

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.