Examples of Python3 file read and write operations

  • 2020-07-21 09:05:28
  • OfStack

Steps of document operation:

Open file - > Operation file - > Close the file

Remember: Close the file last (otherwise unexpected results may occur)

Open the file

File handle = open(' file path ', 'mode ')

Specified file code

File handle = open(' file path ',' mode ',encoding=' ES18en-8 ')

In case you forget to close the file, you can use the context manager to open the file

with open(' file path ',' mode ') as file handle:

The mode to open the file is as follows:

r, read-only mode (default).
w, write only mode. 【 unreadable; create if it does not exist; delete if it does exist; 】
a, append mode. [Readable; created if it does not exist; appended if it does exist;]
r+, can read and write files. 【 readable; writable; appended 】
w +, read write
"U" means that \r \n \r\n can be automatically converted to \n (used with r or r+ modes) when read

rU

r+U

"b" means to handle 2 base files (for example, FTP sends and uploads ISO image files, linux can be ignored, windows should be marked when processing 2 base files)

rb

wb
ab

Close the file

File handle.close()

Operation file:

detach

placeholder

fileno (returns the file descriptor for the I/O operation of the underlying operating system)

fid = File handle.fileno()

print(fid)

flush (flush the buffer, write the data from the buffer to the file immediately)

File handle.flush()

isatty (determines whether the file is connected to a terminal device, returns a Boolean value)

File handle.isatty()

read (reads the specified number of characters from the file, reads all by default)

str = file handle.read() # reads the entire file
str1 = file handle.read(10) # reads the first 10 characters of the file
readable (determines whether the file is readable, returns a Boolean value)

File handle.readable()

readline (read a maximum of 1 row at a time, with a newline '\n' at the end of each row)

print(file handle.readline()) # reads line 1
print(file handle.readline(3)) # reads the first three characters on line 2
print(file handle.readline()) # reads the rest of the characters on line 2
print(file handle.readline()) # read line 3

seek (move the file to read the pointer, if the file contains Chinese, move the pointer must be a multiple of 3, otherwise an error will be reported, because 1 Chinese character is equal to 3 bytes)

File handle.seek(6)

seekable (determines whether a file pointer is available and returns a Boolean value)

File handle.seekable()

tell (get pointer position)

File handle.tell()

truncate (truncate, delete after pointer, and write to file, in writable mode)

f = open (' text txt ', 'r +', encoding = 'utf - 8')
f. seek(9) # moves the pointer after the 9th byte (that is, after the 3rd Chinese byte)
f. truncate() # deletes the character after the third Chinese character and writes to the file
f.close()

writable (determines whether the file is writable, returns a Boolean value)

File handle.writable()

write (writes string to file and returns number of characters)

File handle.write(' string ')


Related articles: