Open the file under python is super easy, no need to import any package, directly enter
f =open('your_file.txt','r')
You can open a file for operation. The second parameter is the operation method of the file,'w' is to write a file, the existing file with the same name will be emptied, and one will be created if it does not exist;'r' is to read the file, if it does not exist, an error will be reported;'a' It is to add content at the end of the file. If it does not exist, the file will be created. If it exists, it will be added directly at the end; there is also'wb' for writing binary files;'rb' for reading binary files, such as pictures.
But this method is not the best way to open files. This method may have several problems:
1、 The file encoding format is not specified. If the file encoding format is inconsistent with the current default encoding format, an error will occur when reading and writing the file content.
2、 If there are errors in reading and writing the file, the file cannot be closed properly. Because even if there is
f.close()
Statement, but if an error occurs when opening, there will be problems with this opening method. Therefore, generally speaking, this method of opening files is not recommended. (I have actually used this method before, although I also know other methods, but it is troublesome and has not been used much...)
The above questions are all for python2. Under python3, the open function can specify the encoding method through the encoding parameter, but not in 2.
Under python3, you can directly open a file like this:
f =open('your_file.txt','r', encoding='utf-8')
The following two methods can be used under both python2 and python3, so if you want to make your code compatible under both 2 and 3, you can try the following two methods:
import codecs
f1 = codecs.open('your_file1.txt','r','utf-8') #Use codecs package
f1.close()import io
f2 = io.open('your_file2.txt','r', encoding='utf-8') #Use io package
f2.close()
I don’t know if any careful students find that the above method of opening files is flawed. The following method is used to solve the second problem just mentioned, and it is the most recommended way to open files.
import codecs #Or io, it doesn’t matter which package is used
with codecs.open('your_file.txt','r','utf-8')as f:
f.write('This method is prior')
Use with this context to open the file, after the file operation is completed, there is no need to close the file through close(), the file will be closed automatically, and the safety factor is higher.
Knowledge point expansion:
The way type opens files in Python
I have been reading "Data Analysis Using Python" these days, and I have encountered a small problem in Chapter 6, data loading, storage, and file format.
Access files in Linux is used:! cat ch06/ex1.csv
Use in the Windows command line:! type ch06\ex1.csv
What needs to be explained is:
The difference between Windows and Linux is that win uses "" to add subdirectories, while Linux uses "/" to add;
You can also use the absolute path to access under win, and copy in the directory where you are in the operation mode. At this time, you need to add quotes to use: type "C:\Users\Burette\pydata-book-master\ch06\ex1.csv"
So far, this article on the ways to open files in python is introduced here. For more information about how to open files with python, please search ZaLou.Cn
Recommended Posts