Python Learning--day17-19 file Operation R W A

Source: Internet
Author: User
Tags throw exception

One, the file operation

Process for file operations

    1. Open the file, get the file handle and assign a value to a variable
    2. Manipulating a file with a handle
    3. Close File

When you open a file, you need to specify the file path and how you want to open the file, and then open it to get the file handle and manipulate it later through the file handle.

The mode of opening the file is:

    • R, read-only mode "default mode, file must exist, not present, throw exception"
    • W, write-only mode "unreadable; not exist" created; empty content "
    • X, write-only mode "unreadable; not present, create, present error"
    • A, append mode "readable; not present, create; append content only"

"+" means you can read and write a file at the same time

    • r+, read and write "readable, writable"
    • w+, write "readable, writable"
    • x+, write "readable, writable"
    • A +, write "readable, writable"

"B" means to operate in bytes

    • RB or R+b
    • WB or W+b
    • XB or W+b
    • AB or A+b

Note: When opened in B, the content read is a byte type, and a byte type is required for writing, and encoding cannot be specified

(1) Examples of read files:

R: First build a file named Test1, write the content Hello World, start reading the file

A=open ("Test1") B=a.read () print (b) a.close ()

The result of the read is:

Hello World

(2) Example of writing files: "W" and "X"

1, W: First build a file, named Test2, write content Welcome to Beijing, start writing content I have a dream

A=open ("Test2", mode= "W", encoding= "Utf-8") b=a.write ("I Have a Dream") print (b) a.close ()

The result of the write is: (Overwrite the previous welcome to Beijing, so W write permission to overwrite the previous content)

I have a dream

2, x: Create an empty file test3, and write content for Hello World

A=open ("Test3", mode= "X") b=a.write ("Hello World") print (b) a.close ()

The result of the write is: (if the file exists, it will be an error, "X" is the need to create a new file)

Hello World

(3) Example of append file:

A: First build a file named Test2, write the content for I have a dream, append new content Hello Xuyaunyaun

A=open ("Test2", mode= "a", encoding= "Utf-8") b=a.write ("\nhello Xuyuanyuan") print (b) a.close ()

The result of the write is appended with the following

I have a Dreamhello Xuyuanyuan

Summary: The mode of opening a file is:

    • R, read-only mode (default).
    • W, write-only mode. "unreadable; not exist; create; delete content;"
    • A, append mode. "Readable; not exist" create; "only append content;"

  

About the readable writable mode:

(1) r+: Create a new file Test1, write the content hello, write the content again Hello Xuyuanyuan

A=open ("Test1", mode= "r+", encoding= "Utf-8") print (A.read ()) b=a.write ("\nhello Xuyuanyuan") print (b) a.close ()

The result of the write is appended with the following Hello

Hellohello Xuyuanyuan

(2) w+: Create a new file Test3, write the content test3, write the content again Goodgirl

A=open ("Test3", mode= "w+", encoding= "Utf-8")
Print (A.read ())
B=a.write ("Goodgirl")
A.seek (0)
Print (A.read ())
A.close ()

The result of the write is: (now delete the previous test3, then write the content Goodgirl) Note: Read content is, because the cursor at this time at the end of the content, there is no content to read, so need to A.seek (0) to move the cursor to the beginning of the position, you can read the content

Goodgirl

(3) A +: Create a new file test4, write the content Xuyuanyuan, write the content again Hello World

A=open ("Test4", mode= "A +", encoding= "Utf-8") b=a.write ("\nhelloworld") A.seek (0) print (A.read ()) A.close ()

The result of the write is: (appended to the content Hello World), note: Read content is, because the cursor at this time at the end of the content, there is no content to read, so need A.seek (0) to move the cursor to the beginning of the position, you can read the content

Xuyaunyuanhelloworld

Summarize:

"+" means you can read and write a file at the same time

    • r+, can read and write files. "readable; writable; can be added"
    • w+, write and read
    • A +, with a

"U" means that the \ r \ n \ r \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ r or r+ mode can be converted automatically when reading

    • RU
    • R+u

About "B" means manipulating in bytes:

(1) RB (Read permissions, in bytes): Create a new file Test1, write the content Hello Xuyuanyuan

A=open ("Test1", mode= "RB") print (A.read ()) A.close ()

The result of the printing is:

B ' Hello Xuyuanyuan '

 

(2) wb+ (read and Write permissions, in bytes): Create a new file Test2, write the content Welcome to Beijing

A=open ("Test2", mode= "wb+") B=a.write (b "Welcome to Beijing") a.seek (0) print (A.read ()) A.close ()

 The result of the printing is:

B ' Welcome to Beijing '

(3)aB + (read and Write permission, append, in bytes): Create a new file Test2, write the content Welcome to Beijing, and append the content welcome Xuyuanyuan

A=open ("Test2", mode= "ab+") B=a.write (b "\nwelcome Xuyuanyuan") a.seek (0) print (A.read ()) A.close ()

 The result of the printing is:  

B ' Welcome to Beijing\nwelcome Xuyuanyuan '

Summarize:

"B" means processing binary files (e.g. FTP send upload ISO image file, Linux can be ignored, Windows process binary file should be labeled)

    • Rb
    • Wb
    • Ab

 

Python file operation Seek (), tell ()

Seek (): Move file read pointer to specified position

Tell (): Returns the location of the file read pointer

 

Python Learning--day17-19 file Operation R W A

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.