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
fileFile location, need to quote
ModeFile open mode, see below 3
BufferingThe desirable values are 0,1,>1 three, 0 for buffer off (only for binary mode), 1 for line buffer (only for text mode), and >1 to indicate the buffer size of the initialization;
encodingIndicates what encoding is used for the returned data, generally using UTF8 or GBK;
ErrorsThe value is generally strict,ignore, when taking strict, character encoding problems, will error, when taking ignore, coding problems, the program will be ignored, continue to execute the following program.
NewLineThe value that can be taken is none, \ n, \ r, ", ' \ r \ n ', to differentiate the newline character, but this parameter is only valid for text mode;
CLOSEFDValue, 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 the Unix kernel system, open a file, A file descriptor is returned.
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 ' /span> |
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; Sho Uld not being 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 or RT default mode, text mode read RB binary file w or wt text mode write, open before file storage is emptied WB binary write, file storage is also emptied a append mode, can only be written at the end of the file A + read-write mode, writing can only be written at the end of the file w+ can read and write, The difference from a + is to empty the contents of the file r+ can read and write, and the difference between A + can be written anywhere in the file
4. Testing
Test file Test.txt with the following content:
Hello,pythonwww.jb51.netthis 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) for L in test: f.write (L) Finally: F.close ()
the difference between A +, w+, and r+ modes (Restore test.txt after test)
A + mode
# cat Test.txthello, Pythonwww.jb51.netThis is a test filetest1test2test3
w+ mode
# Cat Test.txttest1test2test3
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.txttest1test2test3inuxeye.comThis 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 () #读取整个文件, string display ' Hello,python\nwww.jb51.net\nthis is a test file\n ' >>> F.read () #指针在文件末尾, no longer read the content '
>>> f = open (' test.txt ') >>> f.readline () #一次读一行, pointer at the end of the line ' hello,python\n ' >>> F.tell () # The character length of the row 13>>> f.readline () ' www.jb51.net\n ' >>> F.tell () 30>>> f.readline () ' This is a test file\n ' >>> F.tell () 50>>> f.readline () ">>> F.tell () #指针停在最后一行50
>>> f = open (' test.txt ') >>> f.readlines () #读取整个文件, shown in list [' hello,python\n ', ' www.jb51.net\n ', ' This is ' A test file\n ']>>> F.tell () #指针在最后一行50
>>> f = open (' Test.txt ', ' W ') #覆盖创建新文件 >>> f.write (' hello,python! ') #如果写入内容小于1024, there will be memory, otherwise you will need to refresh > >> F.flush () #写入到硬盘 >>> f.close () #关闭文件会自动刷新 >>> f.write (' Hello,linuxeye ') #关闭后, write failed, Hint file has been closed Traceback (most recent call last): File "
", line 1, in
valueerror:i/o operation on CLO SED file