In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the common methods of file objects in python". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deeper to study and learn "what are the common methods of file objects in python" together!
open() method
Python open() method is used to open a file and return the file object. This function is used in the process of processing the file. If the file cannot be opened, OSError will be thrown. (Use the open() method to ensure that the file object is closed, i.e. call the close() method)
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
file: Required, file path (relative or absolute).
mode: optional, file open mode
buffering: Set buffering
encoding: generally utf8
errors: error level
newline: distinguish between newlines
closefd: file parameter type passed in
Common functions of file object close() method
The close() method is used to close an open file. The closed file cannot be read or written, otherwise ValueError will be triggered. The close() method allows multiple calls.
Syntax: fileObject.close();file = open("hello.txt","wb")print(file.name)file.close()#Output: hello.txtread() method
The read() method is used to read the specified number of bytes from a file, or all if not given or negative.
First create your own hello.txt document (custom), the contents of the document is hello world.
file = open("hello.txt", "r")read1 = file.read ()print(read1)file.close()#Output: hello worldwrite() method
The write() method is used to write a specified string to a file.
Write hello world to an empty hello.txt file.
file = open("hello.txt", "w")file.write("hello world")file.close()writelines()
The writelines() method is used to write a sequence of strings to a file. This sequence of strings can be generated by iterating objects, such as a list of strings. New line needs to be formulated\n.
file = open("hello.txt", "w")con = ["a \n", "b\n", "c\n"]file.writelines(con)file.close()#hello.txt write abcflush() method
The flush() method is used to flush the buffer, that is, the data in the buffer is immediately written to the file, and the buffer is emptied at the same time. It is not necessary to wait passively for the output buffer to be written.
Normally, the buffer will be flushed automatically when the file is closed, but if you need to flush it before closing, you can use the flush() method.
file = open("hello.txt", "wb")print("filename: ", file.name)file.flush()file.close()#filename: hello.txtreadline() method
The readline() method is used to read an entire line from a file, including the "\n" character. If a non-negative argument is specified, returns the number of bytes of the specified size, including the "\n" character.
#hello.txt content 123456789file = open("hello.txt", "r")content = file.readline()print(content)file.close()#output hello.txt file first line: 123readlines() method
The readlines() method is used to read all lines (up to the terminator EOF) and return a list that can be processed by Python's for… in …construct. Returns an empty string if EOF is encountered.
file = open("hello.txt", "r")content = file.readlines()print(content)file.close()#Output: ['123\n',' 456\n','789'] seek() method
The seek() method is used to move the file read pointer to a specified location.
fileObject.seek(offset[, whence])
Offset indicates the starting offset, that is, the number of bytes that need to be shifted.
whence: Optional, default is 0. Give the offset parameter a definition of where to start the offset;0 means from the beginning of the file, 1 means from the current position, and 2 means from the end of the file.
Assuming the hello.txt file contains abcdefghijk, let's try moving the file pointer using the seek() method:
file = open("hello.txt", "r")file.seek(3) #file pointer moves to the third position and reads print(file.read()) #output: defghijkfile.seek(5)print(file.read()) #output: fghijkfile.close()tell() method
The tell() method returns the current location of the file, i.e. the current location of the file pointer.
file = open("hello.txt", "r")file.seek(4) #file pointer moved to the third position, read print(file.tell()) # 4file.close()fileno() method
The fileno() method returns an integer file descriptor (FD integer) that can be used for I/O operations on the underlying operating system.
file = open("hello.txt", "w")con = file.fileno()print(con)file.close()#Output: 3isatty() method
Returns True if the file is connected to an end device, False otherwise.
file = open("hello.txt", "w")con = file.isatty()print(con)file.close()#Output: Falsetruncate() method
The truncate() method truncates files to size characters if the optional size parameter is specified. If no size is specified, truncation begins at the current position; all characters after size are deleted after truncation.
#hello.txt text abcdefile = open("hello.txt", "r")con = file.readline()print(con) #Output: afile.truncate() #truncate the rest of the string con = file.readline()print(con) file.close() Thank you for reading, the above is "Python file object commonly used methods what" content, after the study of this article, I believe we have a deeper understanding of Python file object commonly used methods what this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.