Python file operation (basic)

'''File name: small doubleThe contents of the document are as follows: Last night, cicadas couldn't keep singing.It's three o...

'''
File name: small double
The contents of the document are as follows:

Last night, cicadas couldn't keep singing.
It's three o'clock.
Get up and walk around alone.
People are quiet and the moon is dim outside the curtain.
Bai Shou is famous for his work. The old mountain is decorated with pine, which hinders his return.
I want to pay my heart to Yao Qin.
Few bosom friends, who listens to the broken string.
'''

1.

1 f = open('Small pairs', 'r') 2 print(f.readline().strip()) # Read the first line of the file 3 4 # Read the second line when the second reading is performed, because the cursor is automatically placed at the end of the first line after the first reading 5 print(f.readline().strip())

Last night, cicadas couldn't keep singing.

It's three o'clock.

2.

1 f = open('Small pairs', 'r') 2 print(f.readlines()) #Read everything and put it in the list by line 3 f.close()

['last night, cicadas couldn't keep singing. \N ','surprised back to the dream of thousands of miles, has three more. \N ','Get up and walk around alone. \N ',' people are quiet and the moon is dim outside the curtain. \n',

Bai Shou is famous for his work. The old mountain is decorated with pine, which hinders his return. \n ','I want to pay Yao Qin for my troubles. \n ','few bosom friends, who listens to the broken string. ]

3.

1 # Print file content line by line 2 f = open('Small pairs', 'r') 3 4 for n in f.readlines(): 5 print(n.strip()) 6 f.close()
Last night, cicadas couldn't keep singing. It's three o'clock. Get up and walk around alone. People are quiet and the moon is dim outside the curtain. Bai Shou is famous for his work. The old mountain is decorated with pine, which hinders his return. I want to pay my heart to Yao Qin. Few bosom friends, who listens to the broken string.

4.
1 # Add a line of string after line 3 2 f = open('Small pairs', 'r') 3 data = f.readlines() 4 f.close() 5 number = 0 6 7 for n in data: # Print file content line by line 8 if number == 3: 9 n = ''.join((n.strip(),'\n Cutie')) #String splicing.join(),And it can only receive one parameter 10 print(n.strip()) 11 number += 1
Last night, cicadas couldn't keep singing. It's three o'clock. Get up and walk around alone. People are quiet and the moon is dim outside the curtain. Cutie Bai Shou is famous for his work. The old mountain is decorated with pine, which hinders his return. I want to pay my heart to Yao Qin. Few bosom friends, who listens to the broken string. 5.
1 f = open('Small pairs', 'r') 2 3 print(f.tell()) #Display the position of the cursor at this time 4 for n in f: #for Internal will f Object to make an iterator 5 print(n.strip()) 6 print(f.tell())
0 Last night, cicadas couldn't keep singing. It's three o'clock. Get up and walk around alone. People are quiet and the moon is dim outside the curtain. Bai Shou is famous for his work. The old mountain is decorated with pine, which hinders his return. I want to pay my heart to Yao Qin. Few bosom friends, who listens to the broken string. 147

6.
1 f = open('Small pairs', 'r') 2 # gbk Code one Chinese in two places 3 print(f.tell()) #Display the position of the cursor at this time 4 print(f.read(4)) 5 print(f.tell())
0 Last night chilling 8

7.
1 f = open('Small pairs', 'r') 2 # gbk Code one Chinese in two places 3 print(f.read(4)) 4 print(f.tell())#Display the position of the cursor at this time 5 6 f.seek(4) # Adjust cursor position 7 print(f.read(4))
Last night chilling 8 Cold and cicadal

8.
1 f = open('Small pairs', 'a') 2 print(f.isatty()) # Judge whether it is associated with the terminal device, yes True

1 f = open('Small pairs', 'a') 2 3 #Data truncation, in'a'The first six bits of the original data are intercepted in the'w'Format file in mode 4 f.truncate(6)

9.'a +' w + 'r +' mode

f = open('Small pairs', 'w+') #w+Mode, execute the original file format of the statement print(f.readline()) #So the output of executing the statement is null f.write('Cutie') #Rewrite character print(f.readline()) #Reading again is still empty, because the cursor moves to the end after the last sentence operation is completed f.seek(0) # Adjust cursor position print(f.readline()) f.close()

Cutie
1 f = open('Small pairs', 'r+') #r+Pattern 2 3 print(f.readline()) # Output the contents of the original document, 4 5 f.write('Cutie') #Write characters after the contents of the original file 6 print(f.readline()) #The read is empty because the cursor moves to the end after the last sentence is written 7 8 f.seek(0) # Adjust cursor position to 0 9 print(f.readline()) 10 11 f.close()
Cutie Little cute little cute
1 f = open('Small pairs', 'a+') #a+Pattern,Append content; the statement is executed from the cursor to the last position, 2 3 print(f.readline()) # Output is empty, 4 5 f.write('Cutie') #Add content after the content of the original document 6 print(f.readline()) #The read is empty because the cursor moves to the end after the last sentence is written 7 8 f.seek(0) # Adjust cursor position to 0 9 print(f.readline()) #Output existing content 10 11 f.close()

Little cute little cute little cute

2 December 2019, 07:36 | Views: 3498

Add new comment

For adding a comment, please log in
or create account

0 comments