3 Python file operation

Python file operations open The file can be opened in whatever encoding mode the file is stored in f = open('d:\Model housewife nurse head teach...
open
read
write
Append
Modification of documents
Other methods
Python file operations

open

The file can be opened in whatever encoding mode the file is stored in

f = open('d:\Model housewife nurse head teacher.txt',encoding='utf-8') # Absolute path on f.close() f = open('Model housewife nurse head teacher',encoding='utf-8') # Relative path on f.close() with open('log',encoding='utf-8') as f: # This method is often not used to close

read

read

# r read in str f = open('Model housewife nurse head teacher',mode='r',encoding='utf-8') content = f.read() print(content) f.close # rb reads non text content in bytes, which is often used for file upload and download f = open('Model housewife nurse head teacher',mode='rb') content = f.read() print(content) f.close()

readline

Contents of the original document: Hello China I love China China nb #Line by line reading f = open('log',mode='r+',encoding='utf-8') content = f.readline() ා only one line of the file is read print(content) hello China f.close()

readlines

Contents of the original document: Hello China I love China China nb #Read out all the files and display them in list mode f = open('log',mode='r+',encoding='utf-8') content = f.readlines() print(content) ['Hello China \ n ','I love China \ n','China nb '] f.close()

write

# w without this file, it will be created f = open('log','w',encoding='utf-8') f.write('hello Lao Zhang') f.close() # w when this file is available, the contents of the file will be deleted first and then created f = open('log','w',encoding='utf-8') f.write('hello Lao Wang') f.flush() # Force the contents of the buffer to disk f.close() # wb writes binary files in bytes f = open('log',mode='wb') #Write in bytes f.write('Nearby ha ha'.encode('utf-8')) #Convert str to bytes f.close()

Append

# Add only f = open('log','a',encoding='utf-8') f.write('Ha ha ha ha ha') print(f.read()) # The read operation is not available because the cursor is at the end f.close() # Write append w+ f = open('log','w+',encoding='utf-8') f.write('Aoi Sora xixi') f.seek(0) # Adjust the cursor position to read the file ret = f.read() print(ret) f.close() # Read append r + write before read f = open('log','r+',encoding='utf-8') f.write('python') f.seek(0) # Put the cursor at the beginning of the line to read the whole ret = f.read() print(ret) f.close()

Modification of documents

# Principle: # Find the file you want to modify first # Replace what you want to modify in the file # Create a new file # Write new file # Delete source file # rename file # _*_conding:utf-8_*_ import os f_name = 'Head nurse' f_new_name = 'Head nurse.bak' old_str = 'I love China' new_str = 'I love China very much' with open(f_name,encoding='utf-8') as f1,open(f_new_name,'w',encoding='utf-8') as f2: for line in f1: if old_str in line: new_line = old_str.replace(old_str,new_str) f2.write(new_line) os.remove(f_name) os.rename(f_new_name,f_name)

Other methods

f.tell() # Tell you where the cursor is. 0 means the cursor is at the beginning of the line f.readable() # When judging a file, the readable result is True or False f.writable() # Determine whether the file is writable and return True or False f.seek() # Move cursor position f.flush() # Force the contents of the buffer to disk

4 December 2019, 10:29 | Views: 1655

Add new comment

For adding a comment, please log in
or create account

0 comments