14. The path of Python sedimentation--file operation

Source: Internet
Author: User

First, file Operation B mode

1.

1 #f = open (' test11.py ', ' RB ', encoding= ' Utf-8 ') # This situation will be an error2f = open ('test11.py','RB')#b mode cannot specify encoding method3data =F.read ()4 Print('Print it directly:', data)5 Print('decode print \ n', Data.decode ('Utf-8'))6F.close ()
1 Direct print out: B'11111\r\n22222\r\n3333\r\n'2 decoding printing 3  111114 222225 3333

Summary: string-------encoding------->> bytes
Bytes--------decode--------->> strings

Example 2

1f = open ('test22.py','WB')2F.write (Bytes ('99999\n', encoding='Utf-8'))3F.write ('Beijing'. Encode ('Utf-8'))4 f.close ()5 6F1 = open ('test22.py','BR')7data =F1.read ()8 Print('Original Code Printing', data)9 Print('decode print \ n', Data.decode ('Utf-8'))TenF1.close ()

1 Original code print b'99999\n\xe5\x8c\x97\xe4\xba\xac'2 decode print 3  999994 Beijing

Note: The original code printing and decoding the difference between printing, before writing the file, that is, when opened can not specify the encoding format. The encoding format is only specified when writing.

Second, the file operation

1.

1 f = open (' practice. txt','r+', encoding='  Utf-8')2print(F.read ())3 f.write ('  1234556789\n')4print(F.read ())5 F.close ()

1 1234556789

2, closed,encoding, flush (), use of Readlinse ()

1f = open ('practice. txt',"r+", encoding='Utf-8', newline="')2 Print(f.closed)#closed determine if the file is closed3 Print(f.encoding)#check what kind of encoding format4F.flush ()#after writing the content is a temporary existence of the cache inside, through the flush () function flushing, the content is stored in memory,# F.flush () #讲文件内容从内存刷到硬盘5 Print(F.readlines ()) #将所有内容以列表形式打印出来,
1 False 2 utf-83 ['\ r \ n'1234556789\r\n'  1234556789\r\n']

3, the use of Tell, print the current location

1 f = open (' practice. txt','r+', encoding='  Utf-8', newline=')2print(F.tell ())3  Print(F.readline ())     #   because after 12345 there's \ r \ n, not showing up 4  Print(F.tell ())

4. Use of Seek, (in bytes)

1f = open ('practice. txt',"r+", encoding='Utf-8', newline="')2F.write ('My name is Cainiao the from Earth')3 f.close ()4 5F1 = open ('practice. txt','r+', encoding='Utf-8')6 Print(F1.read ())7 8F1.seek (3,0)#"0" represents an offset from the beginning of the file, offset by 3 units9 Print(F1.tell ())Ten Print(F1.read (3)) One Print(F1.read (6))#starts reading 6 characters from the position indicated by the pointer after the offset A Print(F1.tell ()) -  - Print(F1.readline ()) the #print (' see if it's up to the end: ', F1.read (5)) -  - Print(F1.seek (5, 0)) - Print(F1.seek (3,1)) + Print(F1.seek (0,2))#"2" represents an offset of 0 units from the end and "0"

1 Nam 2     Print (F1.seek (3,1)) 3  is C 4 io. Unsupportedoperation:can't do nonzero cur-relative seeks5 6are   from earth7 5

5. Read the last line in a large file

1F=open ('practice. txt','RB')2  forIinchF:3Offs=-104      whileTrue:5F.seek (offs,2)6Data=F.readlines ()7         ifLen (data) > 1:8             Print('the last line of the file is%s'% (Data[-1].decode ('Utf-8')))9              BreakTenoffs*=2

14. The path of Python sedimentation--file operation

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.