Read text file
input = open (' Data ', ' R ')
#第二个参数默认为r
input = open (' Data ')
Read binary files
input = open (' Data ', ' RB ')
Read fixed byte
File_object = open (' Abinfile ', ' RB ')
3. Writing files
Write a text file
Output = open (' Data ', ' W ')
Write a binary file
Output = open (' Data ', ' WB ')
Append Write file
Output = open (' Data ', ' w+ ')
Write Data
File_object = open (' Thefile.txt ', ' W ')
File_object.write (All_the_text)
File_object.close ()
File read/write mode full version:
R+ has read and write properties, start writing from the file header, preserving content that is not covered in the original file;
The w+ has read and write properties, and when written, if the file exists, it is emptied and written from the beginning.
R to open a read-only file, the file must exist.
r+ open a writable file, the file must exist.
W Open Write-only file, if the file exists then the file length is clear to 0, that is, the contents of the file will disappear. If the file does not exist, the file is created.
w+ Open a read-write file, if the file exists then the file length is clear to zero, that is, the contents of the file will disappear. If the file does not exist, the file is created.
A write-only file opens in an additional way. If the file does not exist, the file is created and if the file exists,
The data written will be added to the end of the file, i.e. the original contents of the file will be retained.
A + opens readable and writable files in an additional way. If the file does not exist, the file is created and if the file exists,
The written data is added to the end of the file, i.e. the original contents of the file are retained.
Python common file read/write