Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the IO operations of Python

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what are the IO operations of Python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the IO operations of Python?"

IO programming file read write open file

Open (file, mode='r', buffering=None, encoding=None, errors=None, newline=None, closefd=True) specifically needs to see API. Here are only a few commonly used methods.

The file name of the open function is a required parameter and returns a file object.

# Open a file. F = open ('read.txt', 'r') mode parameter of the open function: value function description' r' read mode'w' write mode'a' append mode'b' binary mode'+ 'read / write mode

Parameter b can deal with other types of files (binaries,) such as mp3 or images without the b parameter in a disk, so add b to the mode parameter

Buffering, file buffer:

Buffer, no buffer by default

If the parameter is 0, the IO operation is unbuffered and the data is written directly to the hard disk.

If the parameter is 1, the data will be buffered and the data will be written to memory first. Only by using the flush function or the close function will the data be updated to the hard disk.

If the parameter is greater than the day, the size of the buffer (in bytes), and-1 (or any negative number) represents the size of the default buffer

File reading

File reading is mainly divided into byte reading and line reading. The commonly used methods are read (), readlines (), close ().

If the text file is opened successfully, then you can write all the contents of the file to memory at once by calling the read () method, and finally return an object of type str: f.read ()

Call close () to close the reference to the file, which must be closed after the file is used, because the file object will occupy the system resources and affect the system IO operation.

Because an IO exception may occur in a file operation, once an IO exception occurs, the subsequent close () method will not be called. So in order to ensure the robustness of the program, we need to use try...finally to implement it.

Try: F = open ('read.txt', 'r') print (f.read ()) finally: if f: f.close ()

Python provides a simple way to write it, using the with statement instead of the try...finally code block and the close () method.

With open ('read.txt', 'r') as fileReader: print (fileReader.read ())

Because the file may be too large and run out of memory, python provides a more reasonable way to call readline () to read one line at a time.

Small files can be read into memory directly by read () method.

A more secure way for large files is to use read (size) continuously.

For text files such as configuration files, it makes more sense to use readline ().

With open ('read.txt', 'r') as fileReader: for line in fileReader.readlines (): print (line.strip ()) get the result: 123456789 file write

Recommended writing method:

With open ('read.txt',') as fileWriter: for num in range (1,100): fileWriter.write (str (num) +'\ n') so far, I believe you have a deeper understanding of "what are the IO operations of Python". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report