preamble
This article is about the use of the open function in python, the usage is as follows:
name = open('','w')<br>()<br>()
1. Look at the first line of code
Used to access the file stored in the disk, you can read and write operations, such as the above example of 'w', here is the file to read operations
Example:
w: open as write
a: opened as an addendum
r+: open in read/write mode
w+: open in read/write mode
rb: open in binary read mode
wb: open in binary write mode
ab: open in binary append mode
rb+: open in binary read/write mode
wb+: open in binary read/write mode
ab+: open in binary append mode
2, we now look at the second line in the above example:
1) First, let's look at the usage and difference between readline and readlines:
>>> name = open('','r') >>> username = () >>> username 'devilf\n' >>> username = () >>> username ['gy\n', 'tom\n', 'lisa\n', 'lebron\n', 'kobe']
As you can see, readline reads only one line, while readlines reads the file line by line.
But we see that the output username list is not a normal list and contains the \n newline character, so we need to remove it:
>>> username = [('\n') for line in ()] >>> username ['devilf', 'gy', 'tom', 'lisa', 'lebron', 'kobe']
3. Third line
In fact, it is a way to close the file after completing the above operations, needless to say
The following is a formal explanation of the use of the open() function.
I. Writing files
1) Write as w
>>> f = open('','w') >>> ('this is a test') 14 >>> () >>> g = open('','r') >>> () 'this is a test'
2) Write as append
>>> f = open('','a') >>> ('2017/1208') >>> () >>> f = open('','r') >>> g = () >>> g 'this is a test2017/1208'
summarize
The above is the entire content of this article, I hope that the content of this article for your study or work has a certain reference learning value, thank you for your support.