File - read, write, append and file operations

 

read file: r

f=open('test.txt',mode='r')
for line in f:
    print(line)
f.close()

Print itself will wrap, and the text comes with a \n, so there is a newline

Binary read: rb

Write file: w, w will create a new file, if there is the same name, delete the content and write it again

f=open( ' test1.txt ' ,mode= ' w ' ,encoding= " gbk " )
 # unicode will automatically transcode bits gbk 
f.write( " unicode encoding is written to gbk file " )
f.close()

Binary write file: wb, wb will also create a file, if there is the same name, delete the content and write it again

Pictures, music, videos, etc. need to be written in wb mode, binary mode

Writing a string to the file in binary mode will result in an error. Prompt requires binary data, not string

f=open( ' test2.txt ' ,mode= ' wb ' )
 # unicode will automatically transcode bit gbk 
f.write( " unicode encoding is written to gbk file " )
f.close()

To solve the above problems, we need to use the encode method. The encode method does not pass parameters. Python3 uses utf-8 for encoding by default.

f=open( ' test2.txt ' ,mode= ' wb ' )
 # unicode will automatically transcode the bit gbk 
f.write( " Write a string to a file in binary mode, you need to encode it first " .encode( ' gbk ' ))
f.close()

File appending: a or ab, if the source file is appended in binary mode, the ab mode must be used, otherwise the characters will be garbled

f=open( ' test3.txt ' ,mode= ' a ' ,encoding= ' gbk ' )
 # unicode will automatically transcode the bit gbk 
f.write( " This is the additional content " )
f.close()

Blend Mode:

  • r+, open the file in read mode, support writing, that is, read and write mode
  • w+, open the file in write mode, support reading, that is, write-read mode, w+ will also clear the original file

File operations:

  • fileno() returns the file handle and the index value in the kernel, which is used by IO multiplexing
  • flush() forces the file to be flushed from the memory buffer to the hard disk, and will be automatically saved to the hard disk when close().

Writing to file, but content open is blank

 After flush(), it will be saved to the hard disk

  • readable() determines whether it is readable or not

Open in w mode to determine whether it is readable

  • readline() reads a line, that is, it encounters the end of \r or \n
  • seek() moves the cursor of the operation file to the specified position

seek is to move the cursor by bytes. As for how many characters to move, it is related to the file encoding. gbk moves two bytes to one character, utf8 moves three bytes to count one character, and read is to read by character

  • seekable() determines whether the file can be seeked, if the device is not seekable
  • tell() returns the current file operation cursor

  • truncate() truncates the file from the specified position to the end, if not specified, it truncates from the current position

  • writable() determines whether the file is writable

Modify the file

First f.seek(), then write() will replace the file, and there may be garbled characters. Therefore, all modifications need to be read into memory and then modified, and then saved and overwritten.

Guess you like

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