File operations, usually open, read, write, append, etc.
#!/usr/bin/env python # coding:utf-8 # # open is actually a request from the OS to get the file handle f = open( ' test ' , encoding= ' utf-8 ' ) # open the file and use the specified encoding data = f.read() # read the file f. close () #Remember to release resources after use print (data) f2 = open( ' t2 ' ) #The default is to read in GBK encoding, so the file needs to be saved in the same encoding print (f2.readable()) #Whether it is readable d2 = f2.readline() #Read one line at a time, it will Including the newline at the end print (d2,type(d2)) print (f2.readline()) print (f2.readlines()) #Read all lines at once, in the form of list f2.close() # # File open mode: basic, read-only, write-only #default is read-only mode, ''' #1. The modes for opening files are (default is text mode): r , read-only mode [default mode, the file must exist, and an exception will be thrown if it does not exist] w, write-only mode [unreadable; create if it does not exist; clear the content if it exists] a, the additional write mode of [unreadable; if it does not exist, create it; if it exists, only append the content] #2. For non-text files, we can only use b mode, "b" means to operate in bytes (and all files are also stored in bytes, using this mode does not need to consider the characters of the text file encoding, jgp format for picture files, avi format for video files) rb wb away Note: When opening in b mode, the read content is byte type, and the byte type needs to be provided when writing, and the encoding cannot be specified #3. Understanding the Section "+" means that a file can be read and written at the same time r+, read and write [readable, writable] w+, write and read [readable, writable] a+, write and read [readable, writable] x, write-only mode [unreadable; create if it does not exist, and report an error if it exists] x+ , write and read [readable, writable] xb For more information, please refer to: http://www.cnblogs.com/linhaifeng/articles/5984922.html#_label2 '''
f = open( ' Test.txt ' , ' w ' ,encoding= ' utf-8 ' ) # f. read() # The write-only file is unreadable print (f. writable()) # Determine whether it is writable f. write( ' 1111111111111\n ' ) #By default, the contents of the file will be emptied and then written. f.write( ' 222\n ' ) f.write('3333333\n') f.writelines([ ' aaaaaaaa\n ' , ' bbbbbb\n ' ]) #Write in the form of a list, and can only be string content. f.close() fo = open( ' Test.txt ' , ' a ' ,encoding= ' utf-8 ' ) # fo. write( ' kkkkkkkk\nbbbbbb\nLet's test everyone. ' ) # print(fo.read()) # unreadable fo.close()
Using the with keyword, open is written differently.
#!/usr/bin/env python # coding:utf-8 #Use the with keyword, the opened file does not need to be closed manually, python will automatically release resources with open( ' t3 ' , ' r ' ,encoding= ' utf-8 ' ) as f: a = f.read() print(a) #You can also open multiple files with with, if a newline ends, it ends with \ # This example automatically reads one file and writes it to another file. and the encoding changes. with open( ' t2 ' , ' r ' ,encoding= ' gbk ' ) as f2, \ open('t4','w',encoding='utf-8') as f3: data = f2.read() f3.write(data)
A small exercise that simulates the process of file modification.
#!/usr/bin/env python # coding:utf-8 # r+ mode is readable and writable, easy to operate, and will not empty the file content. # fop = open('test.txt','r+',encoding='utf-8') # fop.write('9999999999') # If the r+ mode is written at the beginning, it will start from the file header. # c = fop.read() # print(c) # # fop.write('\n\nggggggggggg') # fop.close() # # Simulate the modification process of the file, that is, read the file into the memory first, and then write it to the hard disk after being modified by the program. srf = open( ' t3 ' , ' r ' ,encoding= ' utf-8 ' ) da = srf.readlines() srf.close() print (yes) lis =[] for i in da: a = i.replace('8','') lis.append(a) # print(list(lis)) # dsf = open('t3','w',encoding='utf-8') dsf.writelines(lis) dsf.write('88888888888888\n') dsf.close()