首页 > 编程语言 >Python - File opening modes and buffering

Python - File opening modes and buffering

时间:2024-07-30 14:54:13浏览次数:17  
标签:reading modes Python writing will mode file buffering

'r' - read mode(default)

'w' - write mode

'a' - append mode

'x' - exclusive creation

We know that the mode 'r' opens an existing file for reading only; the file should already exist. If you open a file in this mode, then you cannot write anything to it. It is the default mode, so if you do not provide any mode in the open function then this mode will be used.

The mode 'w' opens a file for writing only, if the file does not exist then it creates a new file, if the file exists, then any content present in the file is erased. You cannot read from a file if you open it in this mode.

The mode 'a' is the append mode. It will also open a file for writing only, but unlike the mode 'w', it will not erase the contents of the file if it already exists. If the file does not exist, then it creates a new file, and if the file exists, then whatever you write to the file will be added at the end of the file. In this mode also, you cannot read from the file.

The mode 'x' is for exclusive creation. It is like the 'w' mode; it creates a new file but fails if the file already exists. So, it will create a new file only if the file with the given name does not exist. If the file exists, then it raises FileExistsError.

You can add a + sign to these modes if you want to perform both reading and writing on the same file. These are called update modes.

'r+' 'w+' 'a+' 'x+'

The mode 'r+' opens a file for both reading and writing and it works only on existing files. It will not create a file if it does not exist.

The mode 'w+' opens a file for both reading and writing. If the file already exists then the data in it is erased, otherwise a new file is created for reading and writing.

The mode 'a+' opens a file for both reading and writing, it will create a new file or append the contents at the end of the file.

The mode 'x+' also opens a file for both reading and writing, and it behaves like the exclusive creation mode.

In Python, files are broadly classified as text files and binary files. You can append letter t or b to the mode strings for working with text or binary files. For example, 'wt' will open a text file for writing, and 'rb' will open a binary file for reading. Text mode is the default, so you can skip the t if you want. Thus, adding a t or nothing means text and adding b means binary.

 

You can control buffering by providing a third argument to the open function. If the third argument is 0, then buffering is disabled and data is transferred immediately to the file. This can reduce performance and it is allowed only in binary mode. If the buffering argument is 1, line buffering is performed which means that the buffer is flushed every time you write a '\n' to the file, this is usable only in text mode. If this argument is any integer greater than one, then buffering is performed with that integer as the buffer size. If a negative value is given or this argument value is not provided in the call, then buffer size is the system default.

标签:reading,modes,Python,writing,will,mode,file,buffering
From: https://www.cnblogs.com/zhangzhihui/p/18332376

相关文章