1. Open () syntax
Open (file[, mode[, buffering[, encoding[, errors[, newline[, Closefd=true]])
The Open function has a number of parameters, commonly used are file,mode and encoding
file location, quotation marks required
mode file, see 3 below
The buffering values are 0,1,>1 three, 0 for buffer off (binary mode only), 1 for line buffer (only for text mode), >1 for the initialized buffer size;
encoding indicates what encoding is used for the returned data, generally using UTF8 or GBK;
errors value generally have strict,ignore, when take strict, when the character encoding problems, will error, when taking ignore, coding problems, the program will be ignored, continue to execute the following program.
NewLine can take a value of None, \ n, \ r, ", ' \ r \ n ', to differentiate between newline characters, but this parameter is only valid for text mode;
The value of the closefd , is related to the incoming file parameters, by default, True, the file parameter passed to the file name, the value is false, file can only be a document descriptor, what is a file descriptor, is a non-negative integer, In a Unix kernel system, a file descriptor is returned by opening it.
2. the difference between file () and open () in Python
Both can open the file, the operation of the file, but also have similar usage and parameters, but, the two file open way there is an essential difference, filefor the document class , the file () to open files , equivalent to this is in the construction of the file class, and open () Opening the file is done using Python's built-in functions , and it is recommended to use the Open
3. Basic values of the parameter mode
| Character |
Meaning |
| ' R ' |
Open for reading (default) |
| ' W ' |
Open for writing, truncating the file first |
| A |
Open for writing, appending to the end of the file if it exists |
| ' B ' |
Binary mode |
| ' t ' |
Text mode (default) |
| + |
Open a disk file for updating (reading and writing) |
| U |
Universal newline mode (for backwards compatibility; Should is used in new code) |
R, W, A is the basic mode of open file, corresponding to read-only, write-only, append mode;
B, T, +, u these four characters, with the above file open mode combination, binary mode, text mode, read and write mode, universal line break, according to the actual situation combination of use,
Common mode value Combinations
r或rt 默认模式,文本模式读
rb 二进制文件
w或wt 文本模式写,打开前文件存储被清空
wb 二进制写,文件存储同样被清空
a 追加模式,只能写在文件末尾
a
+
可读写模式,写只能写在文件末尾
w
+
可读写,与a
+
的区别是要清空文件内容
r
+
可读写,与a
+
的区别是可以写到文件任何位置
4. Testing
Test file Test.txt with the following content:
1 Hello,python 2 www.jb51.net 3 is a test file
Test write files with a small piece of code visually displaying their differences
Test = ["test1\n","test2\n","test3\n"]f= Open ("Test.txt","A +")Try: #f.seek (0) forLinchTest:f.write (L)finally: F.close ()
the difference between A +, w+, and r+ modes (Restore test.txt after test)
A + mode
# Cat Test.txt is a test filetest1test2test3
w+ mode
# Cat Test.txt Test1test2test3
r+ mode
Before writing the file, we add an F.seek (0) to the above code, which is used to locate the write to the file (at the beginning of the file) and directly overwrite the number of characters (note \ n is also a character)
# Cat Test.txt is a test file
Note: This file must exist when the file is opened in r+ mode, otherwise it will be an error, and the ' R ' mode is the same
Other tests
>>> f = open ('Test.txt')>>> F.read ()#read entire file, string display'Hello,python\nwww.jb51.net\nthis is a test file\n'>>> F.read ()#the pointer is at the end of the file and cannot read the contents"'>>> f = open ('Test.txt')>>> F.readline ()#read one line at a time, the pointer at the end of the line'hello,python\n'>>> F.tell ()#character length of a row13>>>F.readline ()'www.jb51.net\n'>>>F.tell ()30>>>F.readline ()'This is a test file\n'>>>F.tell ()50>>>F.readline ()"'>>> F.tell ()#The pointer stops at the last line50>>> f = open ('Test.txt')>>> F.readlines ()#reads the entire file, displays it as a list['hello,python\n','www.jb51.net\n','This is a test file\n']>>> F.tell ()#the pointer is on the last line50
>>> f = open ('Test.txt','W')#Overwrite Create new file>>> F.write ('hello,python!')#If the write content is less than 1024, there is memory, otherwise you need to refresh>>> F.flush ()#write to Hard disk>>> F.close ()#close files are automatically refreshed>>> F.write ('Hello,linuxeye')#write fails after closing, prompting file is closedTraceback (most recent): File"<stdin>", Line 1,inch<module>valueerror:i/O operation on closed file
Python open () File Handling usage Introduction