File operation ②
Binary
mode parameter in the open() method:
- Tread text file (default)
- b read binary
Binary operation by changing mode parameter
file_name = 'DD.jpg' # Read mode # Tread text file (default) # b read binary # t b and r w a are added together. They are all parameters of mode with open(file_name,'rb') as file_obj : ''' //Read the binary code of picture DD.jpg and write it to picture.jpg ''' # When reading a text file, the size is in characters # When reading binary files, size is in bytes # print( file_obj.read(100) ) new_name = 'picture.jpg' with open( new_name ,'wb' ) as new_obj : chunk = 1024*100 # Size per read while True : content = file_obj.read(chunk) # Intermediate variable, storing the read information if not content : # Read empty, read completed break new_obj.write( content ) # Write in
File read location
- seek() and tell() methods adjust and view pointer position
# seek() and tell() with open('demo.txt','rb') as file_obj : # print( file_obj.read(100) ) # print( file_obj.read(20) ) # The seek (offset [, when]) method can modify the position of the current read (pointer) # seek has two parameters: # The first parameter offset is the backward offset relative to a position, which can be negative, and the position is determined by the second parameter # The second parameter is the position calculation method # When = 0 (default), from the beginning to the back; # When = 1, backward from the current position; # When = 2, relative to the move at the end of the file, offset usually takes a negative value file_obj.seek(10) print( 'Current pointer at:',file_obj.tell() ) print( file_obj.read(3) ) file_obj.seek(20,1) print( 'Current pointer at:',file_obj.tell() ) print( file_obj.read(3) ) file_obj.seek(-10,2) print( 'Current pointer at:',file_obj.tell() ) print( file_obj.read(10) ) # The call () method can be used to view the current read (pointer) position print( 'Current pointer at:',file_obj.tell() ) print('- '*20,'I'm a gorgeous dividing line',' -'*20) # When reading Chinese with open('demo2.txt','rt',encoding = 'utf-8') as file_obj : seek_size = 3 file_obj.seek(seek_size) # seek_size is in bytes, a complete Chinese is three bytes, # Therefore, in order to ensure the integrity of the text, it is necessary to report an error read_size = 2 print( file_obj.read(read_size) ) # Because the text file ('rt ', t is the text file) is read at this time, # So read_size is based on characters. A Chinese character is a character print( 'Current pointer at:',file_obj.tell() ) # At this time, the pointer position is 9, 3 + 2 * 3 = 9
Other operations on files
- listdir() returns directory information
- getcwd() get directory information
- chdir() switch to directory
- mkdir() create directory
- remove() delete directory
- rename() renames and moves the path of a file
import os from pprint import pprint # Introducing pprint method from pprint module # os.listdir( path = '.' ) # If a path is required as a parameter, the directory structure under the path will be obtained. The default value is the current directory, # Returns a list of directory structures, each file (folder) is an element in the table r = os.listdir( ) pprint( r ) # os.getcwd() gets the current directory r = os.getcwd( ) print( r ) # Os.chdir (path) switches the current directory # A path is required as a parameter, which will be switched to os.chdir('..') # Go to the parent directory r = os.getcwd( ) print( r ) # os.mkdir('path\name ') create directory (folder) # If the parameter is a folder name directly, it will be created in the current directory # It can also be path + folder name, which is created under the specified path os.mkdir('aa') os.mkdir('06.Exceptions and documents/aa') # os.remove() delete directory # Parameters are the same as above. os.rmdir('aa') os.rmdir('06.Exceptions and documents/aa') os.chdir('06.Exceptions and documents') # Go back to 06. Exception and file directory open('a.txt','x' ) # Create a.txt file # os.remove() delete file # Parameters can be path + filename os.remove('a.txt') # os.rename('old name ',' new name ') can rename and move a file path # Both parameters can be path + filename # open('aa.txt','x' ) os.rename('aa.txt','bb.txt')