English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
A file is a named location on the disk used to store related information. It is used to permanently store data in non-volatile storage (such as hard drives).
Since random access memory (RAM) is volatile, its data will be lost when the computer is turned off, so we use files for future data use.
When we want to read or write to a file, we need to open it first. After completion, it needs to be closed to release the resources bound to the file.
Therefore, in Python, file operations are performed in the following order.
Open the file
Read or write (perform the operation)
Close the file
Python has a built-in function open() to open files. This function returns a file object, also known as a handle, because it is used to read or modify the file accordingly.
>>> f = open("test.txt") # Open the file in the current directory >>> f = open("C:/Python33/README.txt") # Specify the full path
We can specify the mode when opening a file. In the mode, we specify whether to read 'r', write 'w', or append 'a' to the file. We also specify whether to open the file in text mode or binary mode.
The default setting is to read in text mode. In this mode, when reading from the file, we get a string.
On the other hand, binary mode returns bytes, which is the mode to be used when processing non-text files (such as images or exe files).
Mode | Description |
---|---|
'r' | Open the file for reading. (Default) |
'w' | Open the file for writing. If the file does not exist, a new file will be created, or if it exists, it will be truncated. |
'x' | Open the file for exclusive creation. If the file already exists, the operation will fail. |
'a' | Open the file to append at the end without truncating. If the file does not exist, a new file will be created. |
't' | Open in text mode. (Default) |
'b' | Open in binary mode. |
'+' | Open the file for updating (reading and writing) |
f = open("test.txt") # Equivalent to 'r' or 'rt' f = open("test.txt",'w') # Write in text mode f = open("img.bmp",'r+b') # Read and write in binary mode
Different from other languages, the character 'a' does not imply a number before it is encoded in ASCII (or other equivalent encodings).97 .
In addition, the default encoding depends on the platform. In Windows, 'cp'1252But 'utf-8In Linux.
Therefore, we cannot rely on the default encoding either, otherwise the behavior of our code may differ on different platforms.
Therefore, it is strongly recommended to specify the encoding type when handling files in text mode.
f = open("test.txt",mode='r',encoding='utf-8)
After completing file operations, we need to close the file correctly.
Closing the file releases the resources bound to the file and is completed using the close() method.
Python has a garbage collector to clean up unreferenced objects, but we absolutely cannot rely on it to close files.
f = open("test.txt",encoding='utf-8) # Execute file operations f.close()
This method is not completely safe. If an exception occurs while performing certain operations on the file, the code will exit without closing the file.
A safer method is to usetry ... finallyblock.
try: f = open("test.txt",encoding='utf-8) # Execute file operations finally: f.close()
This way, we can ensure that the file is closed correctly even if an exception is raised, causing the program flow to stop.
The best way is to use the with statement. This ensures that the file is closed when the block inside with exits.
We do not need to explicitly call the close() method. It is done internally.
with open("test.txt",encoding='utf-8) as f: # Execute file operations
To write to a file in Python, we can write in 'w' mode, append in 'a' mode, or create exclusively in 'x' mode.
We must be cautious when using the 'w' mode, as it will overwrite the file (if it already exists). All previous data will be deleted.
Writing a string or a byte sequence (for binary files) is done using the write() method. This method returns the number of characters written to the file.
with open("test.txt",'w',encoding='utf-8) as f: f.write("my first file\n") f.write("This file\n\n") f.write("contains three lines\n")
If 'test.txt' does not exist, the program will create a new file with the same name. If it does exist, it will overwrite it.
We must include the newline character ourselves to distinguish different lines.
We must open the file in read mode to read the file using Python.
There are multiple methods available for this purpose. We can use the read(size) method to readsizedata. If not specifiedsizeparameter, which will read and return to the end of the file.
>>> f = open("test.txt",'r',encoding = 'utf-8) >>> f.read(4) # Read the first4data 'This' >>> f.read(4) # Read the next4data ' is ' >>> f.read() # Read the rest until the end of the file 'my first file This file contains three lines ' >>> f.read() # Further reading returns an empty string
We can see that the read() method returns the newline character '\n'. After reaching the end of the file, we will get an empty string when we read further.
We can use the seek() method to change the current file cursor (position). Similarly, the tell() method returns our current position (in bytes).
>>> f.tell() # Get the current file position 56 >>> f.seek(0) # Move the file cursor to the initial position 0 >>> print(f.read()) # Read the entire file This is my first file This file contains three lines
We can usefor loopRead the file line by line. This is both efficient and fast.
>>> for line in f: ... print(line, end = '') ... This is my first file This file contains three lines
The lines of the file itself have a newline character '\n'.
Additionally, the print() function avoids two line breaks when printing.
Alternatively, we can use the readline() method to read individual lines of the file. This method reads the file until the newline character, including the newline character.
>>> f.readline() 'This is my first file ' >>> f.readline() 'This file ' >>> f.readline() 'contains three lines ' >>> f.readline()
Finally, the readlines() method returns a list of the remaining lines of the entire file. When reaching the end of the file (EOF), all these read methods will return an empty value.
>>> f.readlines() ['This is my first file ', 'This file ', 'contains three lines ']
File objects have a variety of available methods. Some of which have been used in the above examples.
This is a complete list of methods in text mode, with brief descriptions.
close() | Close the file. |
detach() | Return the separated raw stream (raw stream) from the buffer. |
fileno() | Return the numeric representation of the stream from the perspective of the operating system. |
flush() | Refresh the internal buffer. |
isatty() | Return whether the file stream is interactive. |
read() | Return the file content. |
readable() | Return whether it is possible to read the file stream. |
readline() | Return a line in the file. |
readlines() | Return the line list in the file. |
seek() | Change the file position. |
seekable() | Return whether the file allows us to change the file position. |
tell() | Return the current file position. |
truncate() | Adjust the file to the specified size. |
writeable() | Return whether it is possible to write to the file. |
write() | Write the specified string to a file. |
writelines() | Write a string list to a file. |