Re-learn Python, learn python

Source: Internet
Author: User

Re-learn Python, learn python
File Read operations

 

Example:

1 print ("-> file handle acquisition, read operation:") 2 3 f = open ('Untitled ', 'R', encoding = 'utf8 ') 4 d = f. read () 5 f. close () 6 print (d) 7 8 print ('-> Example 2:') 9 f = open ('untitle', 'R', encoding = 'utf8 ') 10 e = f. read (9) 11 f. close () 12 print (e) 13 # In python3, both Chinese and English occupy 1 space.

Running result:

-> File handle acquisition and read operations: Last night, stars were painted on the east side of the west side of the building last night, without the colorful Phoenix. the wings were shining with each other.-> Example 2: Last night, stars were painted on the wind.

 

File write operations

 

Knowledge point:

1. Format and clear the file before writing

2. Clear the open w method.

1 print ("write operation, when writing a file, you cannot call the read method. When reading a file, you cannot call the write method") 2 3 f = open ('python ', 'w', encoding = 'utf8') 4 f. write ("I must learn python \ nbecause, python is important \ n") 5 f. write ("java is better? ") 6 f. write (" maybe ") # The preceding statement does not contain line breaks, so the output content is followed by 7 f. close ()

Running result:

After opening the file, the following information is displayed:

I must learn python because, python is important java is better?maybe

 

File append Method

 

Syntax format:

F = open ('filename ', 'A', 'encoding= utf8 ')

This method is used in the append mode: 1. In a blank file, the content is directly written from the beginning; 2. If the file has content, the content will continue to be written at the end.

Example:

F = open ('python', 'A', encoding = 'utf8') f. write ("flowers bloom and bloom") f. close ()

Running result:

I must learn python because, python is important java is better? Maybe flowers bloom and bloom

 

Readline and readlines

Readline reads data row by row.

Readlines is full-text read

Example:

1 print ("readline method") 2 f = open ('untitle', 'R', encoding = 'utf8') 3 a = f. readline () 4 print ("the cursor position at this time:", f. tell () 5 B = f. readline () 6 print ("the cursor position at this time:", f. tell () 7 print (. strip () # strip is the method for removing spaces and line breaks in the string method 8 print (B. strip () 9 10 11 print ("readlines method, the content of each row is printed as a List") 12 f = open ('untitle', 'R ', encoding = 'utf8') 13 c = f. readlines () 14 print (c) 15 print (id (c) 16 print (id (f) 17 for I in c: 18 print (I. strip () 19 print ("Traversal method") 20 f. seek (0) 21 for I in f: 22 print (I. strip () 23 f. close () # In file operations, the close () method must not be forgotten.

Running result:

Readline method: cursor position at this time: 23 cursor position at this time: 46 last night, stars, last night, the wind painted building, the West Side of the guitang East readlines method, the contents of each row are displayed in a list. ['Last night's stars last night wind \ n', 'guitang East \ n' on the west side of the painted building, 'no colorful Phoenix Wings \ n ', 'Hearts and souls] 378268245344280 last night, stars, last night, the wind painted building, the West Side, the east side, the east side, no colorful Phoenix, the wings, the hearts of the soul, the way through the Traversal method last night, the wind painted building, the West Side, the Guangxi Hall, the East Side

 

 

 

Tell () and seek () Methods of Files

Example:

F = open ('untitle', 'R', encoding = 'utf8') f. read (4) print ('current cursor position', f. tell () f. seek (10) print ('current cursor position', f. tell () f. close () # When reading, a Chinese character is counted as three characters

Running result:

Current cursor position 12 current cursor position 10

 

Flush Method for file operations

 

import sys,timefor i in range(20):    sys.stdout.write("#")    sys.stdout.flush()    time.sleep(1)

 

Truncate Method  
F = open ('test', 'w') f. write ("hello") f. write ("\ n") f. write ("python") f. flush () # in this way, data in the memory will be written to diskf without executing the close method. close () f = open ('test', 'A') f. truncate (2) # truncation method. The cursor starts from 2 and then truncate f. close ()

 

 

Other File Methods: r + read/write Methods

 

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.