2018-6-13-python full Stack development day18-file operation

Source: Internet
Author: User

File operations, open files, read files, write files, close files

1. Steps for file operation

Open File

Manipulating files

Close File

2. Open File

  

F=open ('xxx.txt','r', encoding='utf-8 ' ) Data=f.read () f.close

2.1 ReadLine

One row of read files at a time

2.2 Readbale

Determine if a file is readable

2.3 ReadLines

Place the contents of a file in a single element and into a list

2.4 Another form of open file

    

With open ('xxx','r', encoding='utf-8  ') as F

This writing, Python will automatically close the file, no more close

2.5 Writing Files

Write W mode, it is equivalent to create a blank file, write the content before overwriting the original file, in the W when it has been covered out

2.6 Append A

Appends content to the last side of the file and does not delete the contents of the file

2.7 r+,a+,w+

R+ can be read and writable

A + readable writable append

W+ can be read and writable

3.rb

Open a file as a byte

String---Bytes This is what encode wrote me into binary, coded

Bytes---String this is decode to convert the binary into something I can read. decode

Cannot specify encoding when opening in binary form

4. Various methods

4.1 f.encoding ()

What encoding to open when the file is opened

4.2 F.flush ()

Changes to the file in memory, written to the hard disk

4.3 F.tell ()

Displays the location where the cursor is currently located. #除了read是显示光标所在字符的位置, the rest is the display character position

4.4f.seek ()

There are three different modes

4.4.1 0

F.seek (4.0) The first way, from the beginning of the file, to move four bytes

4.4.2 1

F.seek (3.1), moving three bytes from the relative position of the cursor

4.4.3 2

F.seek ( -10,2) move forward 10 bytes from the end of a file

Exercises: Reading the last line of data

F=open ('xxx.txt','rb') offs=-3 while True:    f.seek (Offs,2)    data=f.readlines ()    if len (data) > 1 :         Print (Data[-1].decode ('utf-8'))          Break      *=2

2018-6-13-python full Stack development day18-file operation

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.