Python Learning Diary: day8-------File operations

Source: Internet
Author: User

File operation 1, file path: D:\xxxx.txt absolute path: From the root directory to the last relative path: File 2 in the current directory, encoding: utf-83, how to: Read Only, write, append, read and write, read ... (1) Read-only--r
F =open (' path ', mode='r', encoding=' encoding method ' ) Content=f.read ()print
read-only-RWhat encoding to store the file, it will be encoded in what way to open.  Read-only: R-----> read-only in STR mode; RB------> opens in bytes type for non-text files. (2) write-only-no file is created without this file. A file will first erase the contents of the source file and then write it. Write only: Wf =open (' path ', mode= ' W ', encoding= ' encoded by ')Content=f.write (' content ') f.close ()   wb:f =open (' path ', mode= ' WB ') content=f.write (' content '. Encode (' Utf-8 ')) F.close ()   (3) Append------> Append content after file: af =open (' path ', mode= ' a ', encoding = ' encoding ') f.write (' content ') f.close ()  ABF = Open (' Path ', mode= ' a ') f.write (' content ', encode (' Utf-8 ')) F.close () (4) r+ (read before write) read and write: F = open (' log ', mode = ' r+ ', encoding= ' utf-8 ') Content =fprint (F.read ()) f.write (' content ')  f.close () (5) Write: (read first) f = open (' log ', mode = ' r+ ', encoding= ' utf-8 ') Content =ff.write (' content ')  print (F.read ()) F.close () first write and then read. First, the cursor moves backwards from the beginning, overwriting the contents behind. (6) bytes type of r+ mode: R+BF = open (' log ', mode = ' r+b ') print (F.read ()) f.write (' content '. Encode (' coded ')) F.close () (7) w+f =open (' Path ', mode= ' w+ ', encoding = ' utf-8 ') f.write (' content ') print (F.read ()) F.close () 4, Seek: Adjust cursor f.seek (position)-----"F.seek (0)" Read is reading by character. Seek is the position of the cursor by byte "f =open (' log ', mode = ' r+ ', encodeing= ' Utf-8 ') F.seek (3) content =f.read () print (content) F.close () 5, Breakpoint Continuation----Positioning the position of the cursor F.tell () position of the cursor F =open (' log ', mode = ' A + ', encoding = ' utf-8 ') f.write (' +7 ') Count =f.tell () F . Seek (Count-9) #在uTf-8 a Chinese account of three bytes print (F.read ()) F.close () 6, f.readable () judged is not readable-"returns TRUE or Falseline =f.readline () print (line) f.close ()  7, redline line To line = F.readlines () print (lines) f.close ()   each business into a list of elements, added to the list (lines is a list) truncate intercept a paragraph to read 8, Open file With open (' path ', mode= ' R ', encoding= ' Utf-8 ') as obj:    print (Obj.read ()) opens multiple file encoding two: bytes---" Str:1,decode (decoding) S1 = B.decode (' Utf-8 ') 2, if the string is the letter decoding the time to write GBK will not error
s =abfb=s.encode (' utf-8')print=b.decode ('  gbk')print
encoding and decoding

Python Learning Diary: day8-------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.