Home > Backend Development > Python Tutorial > Detailed explanation of Python3 file operations

Detailed explanation of Python3 file operations

PHPz
Release: 2017-04-04 09:51:44
Original
1821 people have browsed it

Steps of file operation:

Open file-> Operate file->Close file

Remember: Close the file at the end (otherwise there may be unexpected results)

Open file

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

Specify file encoding

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

In order to prevent forgetting to close the file, you can use the context manager to open the file

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

The modes for opening files are:

r, read-only mode (default).

w, write-only mode. [Unreadable; create if it does not exist; delete content if it exists;]

a, append mode. [Readable; if it does not exist, create it; if it exists, only append the content;]

r+, the file can be read and written. [Readable; Writable; Appendable]

w+, write and read

"U" means that \r \n \r\n can be automatically converted to \n when reading (Same as r or r+ mode)

rU

r+U

"b" means processing binary files (such as: FTP sendingupload ISO image file, linux can be ignored, windows needs to be marked when processing binary files)

rb

wb

ab

Close File

File handle.close()

Operation file:

detach

#Placeholder

fileno (returns the file descriptor, used for I/O operations of the underlying operating system)

fid = file handle.fileno()

print(fid)

flush (Refresh the buffer and write the data in the buffer to the file immediately)

File handle.flush()

isatty (Judge the file Whether to connect to a terminal device, return a Boolean value)

File handle.isatty()

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

str = file handle.read() #Read the entire file

str1 = file handle.read(10) #Read the first 10 characters of the file

readable (determine whether the file is readable, return a Boolean value)

File handle.readable()

readline (Read at most one line of data at a time, the end of each line Contains newline character '\n')

print(file handle.readline()) #Read the first line of data

print(file handle.readline(3)) #Read the first line of data The first 3 characters of the second line

print(file handle.readline()) #Read the remaining characters of the second line

print(file handle.readline()) #Read the third line

seek (Move the pointer for file reading. If the file contains Chinese characters, the moving pointer must be a multiple of 3, otherwise an error will be reported, because one Chinese character is equal to 3 bytes)

File Handle.seek(6)

seekable (determine whether the file pointer is available, return a Boolean value)

File handle.seekable()

tell (get the pointer position)

File handle.tell()

truncate (truncate, delete the content behind the pointer and write it to the file, operate in writable mode)

f = open( 'text.txt','r+',encoding='utf-8')

f.seek(9) #Move the pointer to the end of the 9th byte (that is, after the 3rd Chinese character)

f.truncate() #Delete the characters after the third Chinese character and write it to the file

f.close()

writable (determine whether the file is writable, Returns a Boolean value)

File handle.writable()

write (Write string to the file and return the number of characters)

File handle .write('string')

The above is the detailed content of Detailed explanation of Python3 file operations. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template