The difference between python file operation a+ and a mode

(*-*)浩
Release: 2019-06-22 15:26:45
Original
17886 people have browsed it

Regarding several common methods of file operation, there are many explanations on the Internet, and the content is very rich, but it is also somewhat complicated. Today, I will write an article about the difference between a and a based on my personal learning experience.

The difference between python file operation a+ and a mode

‘a’: append writing. If you open an existing file, operate on the existing file directly. (Recommended learning: Python video tutorial)

If the opened file does not exist, create a new file , can only perform writing (append at the end), but cannot read.

‘a ’: append reading and writing. The file is opened and written in the same way as 'a', but can be read. It should be noted that if you just use 'a' to open a file, you generally cannot read it directly, because the cursor is already at the end of the file at this time, unless you move the cursor to the initial position or any non-end position.

>>> fd=open(r'f:\mypython\test.py','a')#附加写方式打开,读取报错
>>> fd.read()
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
IOError: File not open for reading  
>>> fd=open(r'f:\mypython\test.py','a+')
>>> fd.write('123')
>>> fd.read()
>>> fd.close()
Copy after login

For more Python related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of The difference between python file operation a+ and a mode. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!