In Python, read the file using the Open function
1 file=open (r'E:\temp\test.txt','r') 2 var = file.read ()3print(Var)4 file.close ()
The first line opens the E:\temp\test.txt file, noting that the beginning of the string uses R,r to indicate that the content inside the string is not escaped, like @ in C #. In Python, if you want to make the string non-escaped, add R or R at the beginning of the string. The second parameter indicates a read-only file.
| ' R ' |
Read-only |
| ' W ' |
Write only |
| A |
Additional |
| ' B ' |
Binary |
| ' r+ ', ' A + ' |
Update |
The second line reads the contents of the file and assigns the value to the VAR variable.
The third line prints the value of the Var variable.
Line four closes the file.
Read method
| Read () |
Read all, return a string |
| ReadLine () |
Reads only one row at a time, returns a string |
| ReadLines () |
Read all, return to list |
Python learning -13.python input and output (ii)