Python full stack development "seventh" python file operation

Source: Internet
Author: User

First, the document processing process

1. Open the file, get the file handle and assign a value to a variable

2. Manipulating files with a handle

3. Close the file

R mode, default mode, file does not exist error

W mode, file does not exist then created, file exists overwrite

A mode, the file does not exist is created, the file is not overwritten, the content will be written in an additional way (write log file when used), append mode is a special write mode

B (rb,wb,ab) mode: Do not add encoding:utf-8

F=open (' c.txt ', ' RB ') # Print (F.read ()) print (F.read (). Decode ()) F=open (' D.txt ', ' WB ') f.write (' Cheer '. Encode (' Utf-8 ')) F.close ()

Second, the basic operation

1. File Open mode

File handle =open (' File path ', ' mode ')

When you open a file, you specify the file path and how to open the file.

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"
# #只读模式 # F=open (R ' c.txt ', encoding= ' Utf-8 ') # # print (' ====>1 ', F.read ()) # # print (' ====>2 ', F.read ()) # # Print ( F.readable ()) # # Print (F.readline (), end= ") # # Print (F.readline ()) # # print (" = "*20) # # Print (F.read ()) # Print ( F.readlines ()) # F.close () #写模式: file does not exist then create, file exists overwrite original # f=open ("new.py", ' W ', encoding= ' Utf-8 ') # f.write (' 1111111111\n ') # f.writelines ([' 2222\n ', ' 2222548\n ', ' 978646\n ']) # f.close () # Append mode: file does not exist then create, file exists will not overwrite, write content is appended to write # F=open (' new.py ', ' A ', encoding= ' Utf-8 ') # f.write (' nishishui\n ') # f.writelines ([' aa\n ', ' bb\n ']) # f.close ()

"+" 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

Practice, using B-mode, write a CP tool that requires the following:

1. Can copy text and copy video, pictures and other files

# b Mode F=open (' 1.jpg ', ' RB ') Data=f.read () # Print (data) f=open (' 2.jpg ', ' WB ') f.write (data) print (data)

Third, context management

With open (' A.txt ', ' W ')  as  F:passwith open (' A.txt ', ' R ') as  read_f,open (' B.txt ', ' W ')  as  Write_f : Data=read_f.read () write_f.write (data)

iv. Modification of documents  

Import Oswith Open (' A.txt ', ' R ', encoding= ' Utf-8 ') as Read_f,        open (' A.txt.swap ', ' W ', encoding= ' Utf-8 ') as Write_f:    for the Read_f:        write_f.write (line) os.remove (' A.txt ') os.rename ('. A.txt.swap ', ' a.txt ')

Five, the cursor movement within the file

One: Read (3):

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

Second: 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

Seek controls the movement of the cursor as a reference to the beginning of the file.

Tell the position of the current cursor

2. Truncate is truncated file, truncation must be write mode, but can not be opened with W or w+, because that will directly empty the file, so truncate to r+ or a or a + and other modes to test the effect

Import Timewith Open (' test.txt ', ' RB ') as F:    F.seek (0,2) while    True:        line=f.readline ()        if line:            Print (Line.decode (' Utf-8 '))        else:            time.sleep (0.2)

  

  

Python full stack development "seventh" python 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.