Introduction to file opening and closing operation commands in python

不言
Release: 2018-04-27 10:20:47
Original
3025 people have browsed it

The following is an introduction to the file opening and closing operation commands in python. It has a good reference value and I hope it will be helpful to everyone. Let’s take a look together

1. File opening and closing

In python, use the open function to open an existing file. Or create a new file

open(filename, access mode).

 f = open('test.txt', 'w')
Copy after login

File opening mode:

##Open the file in read-only mode. The file pointer will be placed at the beginning of the file. This is the default mode. wOpen a file for writing only. If the file already exists, it is overwritten. If the file does not exist, create a new file. aOpen a file for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, new content will be written after existing content. If the file does not exist, create a new file for writing. rbOpens a file in binary format for reading only. The file pointer will be placed at the beginning of the file. This is the default mode. wbOpens a file in binary format for writing only. If the file already exists, it is overwritten. If the file does not exist, create a new file. ab#Opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, new content will be written after existing content. If the file does not exist, create a new file for writing. r Open a file for reading and writing. The file pointer will be placed at the beginning of the file. w Open a file for reading and writing. If the file already exists, it is overwritten. If the file does not exist, create a new file. a Open a file for reading and writing. If the file already exists, the file pointer will be placed at the end of the file. The file will be opened in append mode. If the file does not exist, a new file is created for reading and writing. rb Opens a file in binary format for reading and writing. The file pointer will be placed at the beginning of the file. wb Opens a file in binary format for reading and writing. If the file already exists, it is overwritten. If the file does not exist, create a new file. ab Open a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, a new file is created for reading and writing.
##Access mode

Instructions

r

Note: When writing to open the file, write to close the file immediately

# 新建一个文件,文件名为:test.txt
f = open('test.txt', 'w')
# 关闭这个文件
f.close()
Copy after login
Related Recommended:


Copy the file to the specified directory with python

How to get the path of the program execution file in python

The above is the detailed content of Introduction to file opening and closing operation commands in python. 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