Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )
Copyright, without permission, is prohibited reprint
chapter
- Python Introduction
- Python development environment to build
- Python syntax
- Python variables
- Python Value Type
- Python type conversion
- Python String (String)
- Operators Python
- Python list (list)
- Python tuples (Tuple)
- Python collection (Set)
- Python dictionary (Dictionary)
- Python If … Else
- Python While loop
- Python For loop
- Python functions
- Python Lambda
- Python Classes and Objects
- Python inheritance
- Python iterator (Iterator)
- Python module
- Python date (Datetime)
- Python JSON
- Python regular expression (RegEx)
- Python PIP package manager
- Python exception handling (Try ... Except)
- Python Open File (File Open)
- Python read file
- Python write file
- Python delete files and folders
File handling is an important part of the application.
Python is included to create, read, update and delete files function.
File Handling
The key document processing functions in Python is a open()
function.
open()
Function takes two filename
parameters: ,mode
Open the file mode (mode) There are four kinds:
r
- read-only mode is the default, if the file does not exist errora
- append mode, you can open the file in append the contents of a file, if the file does not exist, create the filew
- write mode, open the file content is written, if the file does not exist, create the filex
- Create a file, create a file, if the file exists, an error is returned
In addition, you can specify the file processing in binary mode or text mode
t
- Text mode, the default valueb
- binary pattern (e.g., video)
grammar
To open a file for reading, just pass the filename:
f = open("test.txt")
The above code is equivalent to:
f = open("test.txt", "rt")
Because r
(read-only) and t
(text) is the default, so you can not specify.
Note : Make sure the file exists, otherwise the program will report an error.