Example of file read/write operations in Python3,
File Operation steps:
Open a File> operate a File> close a file
Remember: Close the file (otherwise unexpected results may occur)
Open a file
File handle = open ('file path', 'Mode ')
Specify file encoding
File handle = open ('file path', 'Mode', encoding = 'utf-8 ')
To avoid forgetting to close the file, you can use the context manager to open the file.
With open ('file path', 'Mode') as file handle:
File opening modes include:
R, read-only mode (default ).
W, write-only mode. [Unreadable; created if no data exists; deleted if data exists ;]
A. append mode. [Readable; created if it does not exist; appended content if it exists ;]
R +, which can read and write files. [Readable; writable; appendable]
W +, write and read
"U" indicates that \ r \ n can be automatically converted to \ n (used in the same mode as r or r +) during read)
RU
R + U
"B" indicates processing binary files (for example, sending and uploading ISO image files via FTP, which can be ignored in linux and must be noted when processing binary files in windows)
Rb
Wb
AB
Close file
File handle. close ()
Operation file:
Detach
Placeholder
Fileno (return file descriptor, used for underlying operating system I/O operations)
Fid = file handle. fileno ()
Print (fid)
Flush (refresh the buffer and immediately write data in the buffer to the file)
File handle. flush ()
Isatty (returns a Boolean value to determine whether a file is connected to a terminal device)
File handle. isatty ()
Read (reads all specified characters from a file)
Str = file handle. read () # read the entire file
Str1 = file handle. read (10) # read the first 10 characters of the file
Readable (returns a Boolean value to determine whether the object is readable)
File handle. readable ()
Readline (a maximum of one row of data can be read each time, and the last line of each row contains the linefeed '\ n ')
Print (file handle. readline () # Read the first row of data
Print (file handle. readline (3) # read the first three characters in the second row
Print (file handle. readline () # Read the remaining characters in the second line
Print (file handle. readline () # Read the third row
Seek (pointer to be read from a mobile file. If the file contains Chinese characters, the pointer must be a multiple of 3. Otherwise, an error is reported because a Chinese character is equal to 3 bytes)
File handle. seek (6)
Seekable (returns a Boolean value to determine whether the file pointer is available)
File handle. seekable ()
Tell (get pointer position)
File handle. tell ()
Truncate)
F = open('text.txt ', 'r +', encoding = 'utf-8 ')
F. seek (9) # Move the pointer to the end of 9th bytes (after 3rd Chinese characters)
F. truncate () # Delete the characters following the 3rd Chinese characters and write them to the file.
F. close ()
Writable (returns a Boolean value to determine whether a file can be written)
File handle. writable ()
Write (write the string to the file and return the number of characters)
File handle. write ('string ')