The basics of Python Learn the operation of the Nineth day file

Source: Internet
Author: User

1, file operation

Parameters: 1, File path 2, encoding mode, 3, performing action (open mode), read only, write-only, read-write, append and read-write.

1 Open the file, get the file handle and assign a value to a variable. 2. Manipulate the file through a handle. 3. Close the file.

f = open ('yuyu', encoding='gbk', mode='R '  = f.read () print (s) f.close ()

Open is the system feature of Windows, and the default encoding for Windows is Gbk,linux operating system defaults to Utf-8.

Process: Opens a file, generates a handle, operates on a file handle, and then closes.

READ: R read-only

1, what encoding the file is stored in what encoding is opened.

2, File path: Absolute path, starting from the root directory first level lookup until found.

Relative paths, write files directly under the same folder.

Five different ways:

1,f.read, read them all. Read-only mode, can not be noted. The default is read-only mode.

F=open (' Yu.bak ', encoding= ' utf-8 ')

2,f.readline, a row of reads.

3,f.readlines reads each line of the original file as an element of a list.

4,f.read (n) is read by character in R mode. n= (number of characters)

In RB mode read (n) reads in bytes. RB mode is read-only with bytes type, non-text class file with RB

5, Loop read. This is best, and takes up less memory.

f = open ('yu','r', encoding='utf-8'  ) for in F:    print(I.strip ()) F.close ()

Write: W,write write mode

f = open ('yu','w', encoding='utf-8') = F.write (' when the moon has ')print(s) f.close ()   

W write mode, no files when creating a file to write content, there is a file, the original file content is emptied, and then write the content.

#1. The mode of opening the file has (default is text mode):R, read-only mode "default mode, the file must exist, does not exist" throw exception "W, write-only mode" is not readable; "A, append write mode is not readable;
F=open ('yu.bak', mode='rb') s= F.read (6) Print (s) f.close () #b ' alex\xe6\x98 '

Wbab Note: When opened in B, read the content is byte type, write also need to provide byte type, can not specify the encoding # #, ' + ' mode (is added a function) r+, read and write "readable, can write" w+, write read "writable, readable" A +, write read "writable, readable" # 4, read/write, read, write, read r+b, read and write "readable, writable" w+b, write read "writable, readable" a+b, write read "writable, readable" by bytes type operation

  

1. When the file is opened in text mode, the representation reads 3 characters

2. When the file is opened in B mode, the delegate reads 3 bytes

The rest of the files within the cursor movement are in bytes such as:seek,tell,truncate

Attention:

1. Seek has three modes of movement 0,1,2, of which 1 and 2 must be performed in B mode, but regardless of which mode is moved in bytes units

2. Truncate is a truncated file, so the file must be opened in a way that can be written, but not with W or w+, etc., because the file is directly emptied, so truncate to r+ or a or a + and other modes to test the effect.

f = open ('yu','r+', encoding='utf-8') = f.truncate (4)print(s) f.close () 
2. Modification of files

#① Create a new file
#② read an original file
#③ the contents of the original file in the way you want to make changes and write to the new file
#④ Delete the original file
#⑤ Renaming a new file

With statement:

1, function One automatically closes the file handle. 2. Operate multiple files at once.

# Import OS # with open (' Yu ', encoding= ' Utf-8 ') as f1,\ #      Open (' Yu.bak ', encoding= ' utf ', mode= ' W ') as F2:#     content = F1.read ()#      new_content = content.replace (' SB ', ' Alex ')#     f2.write (new_content) # os.remove (' Yu ')

The basics of Python Learn the operation of the Nineth day file

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.