Python learning ==> file operation

Basic file operations:

m = open('words',encoding='utf-8')#Open the file (python default character set is utf-8, windows is gbk), no input mode defaults to r mode 
print(m.read()) # Read the file
print(m.readline()) #Read the first line
print(m.readlines()) #Put each line of the file into a list

The mode in which the file is opened:

r, read-only mode (default) [not writable; if the file does not exist, an error will be reported] 
w, write-only mode [unreadable; if it does not exist, create it; if it exists, delete the content;]
a, append mode [unreadable; if it does not exist, then Create; only append the content if it exists;]
r+, read and write mode [readable, writable; append, if the open file does not exist, an error will be reported]
w+, write and read mode [if w+ is used, the content of the existing file It will be emptied, and the content of the file that has been written can be read]
a+, additional read-write mode [create if it does not exist; only append the content if it exists;]

File operation method:

f = open('file.txt','r+',encoding='utf-8') # The encoding parameter can specify the encoding of the file 
f.readline() # Read a line f
. readable() # Determine whether the file is readable
f . .writable() # Determine whether the file is writable
f.encoding # Print the encoding of the file
f.read() # Read all the content, do not use it when large files, because the content of the file will be read into the memory, if the memory is not enough, It will burst the memory
f.readlines() # Read all file contents, return a list, the element is the data of each line, don't use it for large files, the reason is the same as above
f.tell() # Get the pointer to the current file
f.seek(0) # Where the current file pointer points to
f.write('love certificate') # Write the content
f.fulsh() # After writing the file, immediately write the data from the memory to the disk
f.truncate () # Clear the file content
f.writelines(['love certificate' ,'Sun Yanzi']) #Write a list to the file
f.close() #Close the file

Efficient operation method for reading large files:

If you use the above read() and readlines() methods to operate the file, all the contents of the file will be read into the memory first. In this case, the memory data is too much, which is very stuck, and the efficient operation is to read one line and operate one line. The content is released from memory as follows:

f = open('file.txt') 
for line in f:
print(line)
# In this case, line is the content of each line of the file. After reading a line, the memory of a line will be released

file pointer:

# Used to record where the file was read 
m = open('name','a+',encoding='utf-8')# The file pointer for a mode is at the end
m.write('uuuuuu')
m. seek(0) #Move the file pointer to the front
print(m.read())
m.write('Hehehe') #After moving the file pointer, start reading from the front, but still write
m at the end of the file when writing .seek(0)
print(m.read())

Automatically close files:

When working with files, it is often forgotten to close the file. You can use with, which will automatically close the file after using the file handle, as follows:

with open('file.txt', 'r') as f:# Open a file and give f the handle of the file 
for line in f:
print(line)
with open('file.txt') as fr, open('file_bak', 'w') as fw:# This is a multi-file operation, open two files, fr is to read file.txt, fw is to create a new file_bak file
for line in fr:# Loop in file.txt Each line of
fw.write(line)# is written to the file_bak file

Modify the file:

There are two ways to modify the file:

       1. One is to read the entire contents of the file into the memory, then clear the original contents of the file, and rewrite the new contents.

       2. The second is to write the contents of the modified file to a new file.

The first way:

with open('file.txt','r+') as fr:
res = fr.read()
new_res = res.replace('我' ,'me')
fr.truncate()
fr.write(new_res)

The second way:

# This is a multi-file operation, open two files, fr is to read file.txt, fw is to create a new file_bak file 
import os
with open('file') as fr,open('new_file','w') as fw :
for line in fr: # Loop each line in file.txt
new_line = line.replace('flower','flower')
fw.write(new_line) # Write to file_bak file
os.remove('file') # Delete file
os.rename('new_file','file') # rename

f.write()和f.writelines

# f.write() can only write strings 
a = ['abc\n','123\n','!@#']
f = open('name','w')
for i in a:
f .write(i)

# f.writelines() will loop and write the elements in the list to the file
a = ['abc\n','123\n','!@#']
f = open('name', 'w')
f.writelines(a)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325277223&siteId=291194637