Read files commonly used in Python programs:
f = open ("___", ' R ')
For line in F: #这里每次读取文件的一行, line is a string and the end of the string includes ' \ n '!!!
Print Line
F.close ()
Transferred from: http://www.jb51.net/article/58002.htm
1. Open () syntax
Open (file[, mode[, buffering[, encoding[, errors[, newline[, Closefd=true] []]]])
The Open function has many parameters, Commonly used is the File,mode and encoding
file files location, which need to be quoted
mode File open mode, see below 3
Buffering has a 0,1,>1 of three, 0 for buffer off (only for binary mode), 1 for line buffer (only for text mode), >1 for the initialized buffer size;
encoding represents the encoding of the returned data, generally using UTF8 or GBK;
Errors has a general value of Strict,ignore, When taking strict, when the character encoding problems, will be 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 valid only for text mode;
closefd , is related to the file parameters passed in, 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 ' |
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
?
1 2 3 4 5 6 7 8 9 Ten One |
r或rt 默认模式,文本模式读 rb 二进制文件 w或wt 文本模式写,打开前文件存储被清空 wb 二进制写,文件存储同样被清空 a 追加模式,只能写在文件末尾 a + 可读写模式,写只能写在文件末尾 w + 可读写,与a + 的区别是要清空文件内容 r + 可读写,与a + 的区别是可以写到文件任何位置 |
4. Testing
Test file Test.txt with the following content:
?
1 2 3 |
Hello,Python www.jb51.net This 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
?
2 4 6 7 |
# Cat Test.txt hello, Python www.jb51.net this is a test file Span style= "Font-family:times new Roman,times; font-size:18px; " >test1 < Code class= "Py Plain" >test2 < Code class= "Py Plain" >test3 |
w+ mode
?
1 3 4 |
< Span style= "Font-family:times new Roman,times; font-size:18px; " ># cat test.txt test1 < Code class= "Py Plain" >test2 < Code class= "Py Plain" >test3 |
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)
?
1 2 3 4 5 6 |
# Cat Test.txt test1 test2 Span style= "Font-family:times new Roman,times; font-size:18px; " >test3 < Code class= "Py Plain" >inuxeye.com this 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
1 2 3 4 5 6 7 8 |
>>> f = open ( ‘test.txt‘ , ‘w‘ ) #覆盖创建新文件 >>> f.write( ‘Hello,Python!‘ ) #如果写入内容小于1024,会存在内存,否则需要刷新 >>> f.flush() #写入到硬盘 >>> f.close() #关闭文件会自动刷新 >>> f.write( ‘Hello,Linuxeye‘ ) #关闭后,写失败,提示文件已经关闭 Traceback (most recent call last): File "<stdin>" , line 1 , in <module> ValueError: I / O operation on closed file |
Linux open () file operations