What are the ways to open files in python

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:

  1. The difference between Windows and Linux is that win uses "" to add subdirectories, while Linux uses "/" to add;

  2. 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

What are the ways to open files in python
​What are the numbers in Python?
3 ways to encrypt Python files
What are the methods for python to connect to mysql
What are the required parameters of python
How to delete cache files in python
How to save text files in python
What are web development frameworks in python
What are the advantages of python language
How to open python in different systems
Python example method to open music files
What is the function of adb in python
How to use the zip function in Python
An article to understand the yield in Python
What does the tab key in python mean
How to use the format function in python
How to open the ubuntu system in win10
How to delete files and directories in python
How to install the downloaded module in python
The best way to judge the type in Python
What conditions are needed for infinite loop in Python
Python data visualization: who are the big names in Python?
Python implements FTP to upload files in a loop
What are python class attributes
What is introspection in python
What is object-oriented in python
How to find the area of a circle in python
What is list comprehension in python
How to wrap in python code
What does rc1 mean in python
How to save the python program
What does def in python do
What is the use of Python
How to omit parentheses in Python
How to write classes in python
How to filter numbers in python
There are several keywords in Python
How to view errors in python
What does np do in python
How to write return in python
Talking about the modules in Python
How to view the python module
How to understand variables in Python
How to clear variables in python
The usage of tuples in python
How to use SQLite in Python
Understanding the meaning of rb in python
How to use and and or in Python
How to introduce third-party modules in Python
How to represent null values in python
The usage of Ajax in Python3 crawler
Python string to judge the password strength
What is the scope of python variables
How to write win programs in python
How to run id function in python
How to install third-party modules in Python
What is the id function of python
What is an anonymous function in Python
Python | So collections are so easy to use! !
How to define private attributes in Python
How to add custom modules in Python