How to read files in Python? In this article, let's learn about Quantrimang.com How to read Python files okay!
File processing in Python is a powerful and flexible tool that can be used to carry out a variety of different activities. However, it's important to carefully consider the pros and cons of file handling when writing Python programs, to ensure that the code is safe, reliable, and well-implemented.
Manipulating files in Python
Python also supports file handling and allows users to manipulate files, for example reading and writing files, along with many other file handling options to manipulate files. The concept of file handling exists in almost every programming language, but the implementation is quite complicated and lengthy. However, like other concepts in Python, the concept of file handling is also easy and concise.
Python handles different files as text or binary. This is very important. Each line of code consists of a string of characters and they form a text file. Each line of the file ends with a special character, named EOL or a line ending character such as a comma or newline character. It terminates the current line and tells the interpreter that a new line has begun. Now let's learn how to read data from files in Python.
What is file?
File, also known as file, file. A file is a collection of information named and stored on computer memory such as hard disk, floppy disk, CD, DVD,…
When we want to read or write a file, we need to open the file first. When completed, the file needs to be closed so that the resources attached to the file are released.
Therefore, in Python, a file operation takes place in the following order.
- Open the file
- Read or write
- Close the file
Open File in Python
In Python, there is a built-in function for opening files: open(). This function returns a file object or “handle” because you can perform read, write, and modify operations on that file.
>>> f = open("test.txt") # mở file cùng thư mục với file hiện tại
>>> f = open("C:/Python33/README.txt") # mở file ở thư mục khác, đường dẫn đầy đủ
You can determine how the file is opened for what purpose, such as read, write, append, etc. This is an optional parameter that may or may not be present. In addition, you can also specify whether the file opens in text or binary format.
The default file access mode is read (r). When using this mode, we will receive the return string value as text.
On the other hand if the return value is in bytes then the file opened is an image or exe.
Below is a list of different modes when opening a file:
MODE | DESCRIBE |
'r' | Read-only mode. |
'r+' | Read and write modes are allowed |
'rb' | Open the file in read mode for binary format. Pointer at the start of the file |
'rb+' 'r+b' |
Open file for reading and writing in binary format. Pointer at the start of the file |
'w' | Open the file for recording. If the file does not exist, a new file will be created and the content will be written. If the file already exists, the file will be truncated and the old content will be overwritten. |
'w+' | Open file for reading and writing. If the file does not exist, a new file will be created and the content will be written. If the file already exists, the file will be truncated and the old content will be overwritten. |
'wb' | Open the file to write to binary format. If the file does not exist, a new file will be created and the content will be written. If the file already exists, the file will be truncated and the old content will be overwritten. |
'wb+' 'w+b' |
Open the file to read and write to binary format. If the file does not exist, a new file will be created and the content will be written. If the file already exists, the file will be truncated and the old content will be overwritten. |
'a' | Open the file in recording mode. If the file already exists, it will continue to write the content to the end of the file. If the file does not exist, create a new file and write the content there. |
'a+' | Open the file in read and write mode. If the file already exists, it will continue to write the content to the end of the file. If the file does not exist, create a new file and write the content there. |
'ab', | Open the file in binary format. If the file already exists, it will continue to write the content to the end of the file. If the file does not exist, create a new file and write the content there. |
'ab+' 'a+b |
Open the file in read mode and continue writing in binary form. If the file already exists, it will continue to write the content to the end of the file. If the file does not exist, create a new file and write the content there. |
'x' | Open the file in recording mode. Create a new exclusive file (exclusive creation) and write the content. If the file already exists, the program will report an error |
'x+' | Open the file in read and write mode. Create a new exclusive file (exclusive creation) and write the content. If the file already exists, the program will report an error |
'xb' | Open the file in binary recording mode. Create a new exclusive file and write the content. If the file already exists, the program will report an error |
'xb+' 'x+b' |
Open the file in binary read and write mode. Create a new exclusive file and write the content. If the file already exists, the program will report an error |
'b' | Open the file in binary mode |
't' | Open file in text mode (default) |
f = open("test.txt") # mở file mode 'r' hoăc 'rt' để đọcf = open("test.txt",'w') # mở file mode ‘w’ để ghi
f = open("img.bmp",'r+b') # mở file mode ‘r+b’ để đọc và ghi dạng nhị phân
When working with files in text mode, you should specify the encoding type.
f = open("test.txt",mode="r",encoding = 'utf-8')
Close File in Python
After completing operations with the file, you need to close it.
Closing the file ensures opening and closing regulations and frees memory for the program, so this is necessary.
Closing files is built into Python using functions close().
Python also automatically closes a file when the file's reference has been reassigned to another file. However, using method close() To close a file is still better.
f = open("test.txt",encoding = 'utf-8')
# thực hiện các thao tác với tệp
f.close()
However, this method is not really guaranteed. There are still cases where some exceptions occur when we perform operations with files, causing the program to automatically exit without closing the file.
For extra security, you should use the try…finally block (finally will always be executed regardless of whether there are exceptions) here.
try:
f = open("test.txt",encoding = 'utf-8')
# thực hiện các thao tác với tệp
finally:
f.close()
This way, we can be sure the file is closed correctly even if an exception occurs that causes the program to suddenly stop.
Another way to close a file is to use the command with. Command with gives us a guarantee that the file is always closed without knowing the internal processing logic.
with open("test.txt",encoding = 'utf-8') as f:
# thực hiện các thao tác với tệp
Comparing these two ways of writing, we can see very clearly that, using with gives us a much more concise way to write code.
Write File in Python
To write a file we need to open the file using the write syntax, use write mode 'w', append 'a' or create exclusive mode 'x'
You need to be careful with 'w' mode, because it overwrites the content if the file already exists, the previous data will be deleted.
If you write text strings or byte strings to a binary file, the return result will be the number of characters written to the file.
with open("test.txt",'w',encoding = 'utf-8') as f:
f.write("Quantrimang\n")
f.write("Kiến thức - Kinh nghiệm - Hỏi đáp\n\n")
f.write("Quantrimang.com\n")
With the above example, the program will create a file named test.txt If the file does not exist, it will be overwritten.
Use characters '\n' to distinguish lines from each other.
The results returned are:
Quantrimang
Kiến thức - Kinh nghiệm - Hỏi đáp
Quantrimang.com
Read File in Python
Similar to writing a file, to read a file we need to open the file using the read syntax, using read 'r' mode.
Use read(size)
Use method read(size) to retrieve data sized by size. If this parameter is left blank, it will read the entire file or if the file is too large, it will read until the memory limit allows.
f = open("test.txt",'r',encoding = 'utf-8')
a = f.read(12) # đọc 12 kí tự đầu tiên
print('Nội dung 11 kí tự đầu là:\n', (a))
b = f.read(35) # đọc 35 kí tự tiếp theo
print('Nội dung 35 kí tự tiếp theo là:\n', (b))
c = f.read() # đọc phần còn lại
print('Nội dung phần còn lại là:\n', (c))
Run the program, the returned results are:
Nội dung 12 kí tự đầu là:
Quantrimang
Nội dung 35 kí tự tiếp theo là:
Kiến thức - Kinh nghiệm - Hỏi đáp
Nội dung phần còn lại là:
Quantrimang.com
Using tell() and seek()
Additionally, we have method tell() tells you the current location within the file. In other words, subsequent reads and writes will continue on those bytes.
Method seek() Change the current position within the file.
f = open("test.txt",'r',encoding = 'utf-8')
a = f.read(12) # đọc 12 kí tự đầu tiên
print('Nội dung là: \n', (a))
b = f.tell() # Kiểm tra vị trí hiện tại
print ('Vị trí hiện tại: ', (b))
f.seek(0) # Đặt lại vị trí con trỏ tại vị trí đầu file
c = f.read()
print('Nội dung mới là: \n', (c))
Returned results:
Nội dung là:
Quantrimang
Vị trí hiện tại: 13
Nội dung mới là:
Quantrimang
Kiến thức - Kinh nghiệm - Hỏi đáp
Quantrimang.com
Use readline()
This method allows reading each line in the file:
f = open("test.txt",'r',encoding = 'utf-8')
a = f.readline()
print ('Nội dung dòng đầu: ', (a))
b = f.readline()
print ('Nội dung dòng 2: ', (b))
c = f.readline()
print ('Nội dung dòng 3: ', (c))
d = f.readline()
print ('Nội dung dòng 4: ', (d))
Results printed to the screen:
Nội dung dòng đầu: Quantrimang
Nội dung dòng 2: Kiến thức - Kinh nghiệm - Hỏi đáp
Nội dung dòng 3:
Nội dung dòng 4: Quantrimang.com
Use readlines()
Method readlines() Returns all remaining lines in the file and returns empty at the end of the file.
f = open("test.txt",'r',encoding = 'utf-8')
a = f.readline()
print ('Nội dung dòng đầu: ', (a))
b = f.readlines()
print ('Nội dung các dòng còn lại: \n', (b))
c = f.readlines()
print ('Nội dung các dòng còn lại: \n', (c))
Results displayed on the screen:
Nội dung dòng đầu: Quantrimang
Nội dung các dòng còn lại:
['Kiến thức - Kinh nghiệm - Hỏi đáp\n', '\n', 'Quantrimang.com\n']
Nội dung các dòng còn lại:
[]
Some methods for working with Files in Python
There are many different methods for working with files built into Python, some of which have been learned by Quantrimang above.
The table below is a complete list of methods in text format, you can refer to it for more information.
METHOD | DESCRIBE |
close() | Close an open file. It cannot be executed if the file has been closed. |
fileno() | Returns an integer describing the file (file descriptor). |
flush() | Clear the file stream cache. |
isatty() | Returns TRUE if the file is connected to a terminal. |
read(n) | Read n characters in the file. |
readable() | Returns TRUE if the file is readable. |
readline(n=-1) | Reads and returns a line from the file. Reads at most n bytes/characters if specified. |
readlines(n=-1) | Reads and returns a list of lines from the file. Reads at most n bytes/characters if specified. |
seek(offset,from=SEEK_SET) | Change the current position within the file. |
seekable() | Returns TRUE if the stream supports random access. |
tell() | Returns the current position within the file. |
truncate(size=None) | Shred the file size to the size parameter size. |
writable() | Returns TRUE if the file is writable. |
write(s) | Write s characters into the file and return it. |
writelines(lines) | Writes a list of lines and files. |
Advantages and disadvantages of reading data from files in Python
Advantage
- Flexible: File handling in Python allows you to perform a series of operations such as creating, reading, writing, adding, renaming, and deleting files.
- Flexible: File handling in Python is highly flexible because it allows you to work with different file types (e.g. text files, binary files, CSV files…) and to perform different tasks on file (eg: read, write, add…).
- User-friendly: Python provides a user-friendly interface for file handling, making it easy to create, read, and edit files.
- Cross platform: File handling functions in Python work across different platforms (e.g. Windows, Mac, Linux), allowing for seamless integration and compatibility.
Disadvantages
- Prone to errors, especially when the code is not written carefully or the system has problems.
- Potential security risks.
- Complicated, especially when working with advanced file formats.
- Processing speed is slower than other programming languages.
See more:
Previous lesson: Matrix in Python
Next article: Managing files and directories in Python