SoFunction
Updated on 2024-10-29

Summary of Python methods for reading and writing files

This article example summarizes Python read and write file methods. Shared for your reference. Specific analysis is as follows:

Always remember to call the file object's close() method after opening the file with open. For example, you can use a try/finally statement to make sure that you finally close the file.

file_object = open('')
try:
   all_the_text = file_object.read( )
finally:
   file_object.close( )

Note: You can't put the open statement in a try block because the file object file_object can't execute the close() method when an exception is thrown for opening the file.

2. Read the file

Read text files

input = open('data', 'r')
# The second argument defaults to r
input = open('data')

Read binary files

Copy Code The code is as follows.
input = open('data', 'rb')

Read all

file_object = open('')
try:
   all_the_text = file_object.read( )
finally:
   file_object.close( )

Read Fixed Bytes

file_object = open('abinfile', 'rb')
try:
  while True:
     chunk = file_object.read(100)
    if not chunk:
      break
     do_something_with(chunk)
finally:
   file_object.close( )

read each line

Copy Code The code is as follows.
list_of_all_the_lines = file_object.readlines( )

If the file is a text file, you can also traverse the file object directly to get each line:

for line in file_object:
   process line

3. Write file

Write text files

Copy Code The code is as follows.
output = open('data', 'w')

Writing binary files

Copy Code The code is as follows.
output = open('data', 'wb')

Additional writing of documents

Copy Code The code is as follows.
output = open('data', 'w+')

write data

file_object = open('', 'w')
file_object.write(all_the_text)
file_object.close()

Write multiple lines

Copy Code The code is as follows.
file_object.writelines(list_of_text_strings)

Note that calling writelines to write multiple lines will be higher in performance than writing them all at once using write.

In the processing of log files, often encounter such a situation: the log file is huge, it is not possible to read the entire file into memory at once for processing, for example, the need to process a 2GB log file on a machine with 2GB of physical memory, we may want to process only 200MB of the content each time.
In Python, the built-in File object provides a readlines(sizehint) function directly to do things like this. Take the following code as an example:

file = open('', 'r')
sizehint = 209715200  # 200M
position = 0
lines = (sizehint)
while not () - position < 0:
    position = ()
    lines = (sizehint)

Each call to the readlines(sizehint) function returns about 200MB of data, which is always a full line of data, but in most cases the number of bytes of data returned is slightly larger than the sizehint value (except for the last call to the readlines(sizehint) function). Typically, Python automatically adjusts the user-specified value of sizehint to an integer multiple of the internal cache size.

file is a special type in python which is used to manipulate external files in python programs. Everything is an object in python, and file is no exception. file has the methods and properties of file. Let's see how to create a file object first:

file(name[, mode[, buffering]])
The file() function is used to create a file object, it has an alias called open(), perhaps more figuratively, they are built-in functions. Let's take a look at its parameters. The parameters are passed as strings. name is the name of the file.
mode is the mode in which the file will be opened. The available values are r w a U, which stands for read (the default) Write Adds modes that support various line breaks. If a file is opened in w or a mode, it is automatically created if it does not exist. In addition, when opening a file that already exists in w mode, the contents of the original file will be cleared, because the mark for the file operation is at the beginning of the file at the beginning of the file, and a write operation at that time will undoubtedly erase the original contents. Due to historical reasons, line breaks in different systems have different modes, for example, in unix is a /n, and in windows is '/r/n', with the U mode to open the file, that is, to support all line breaks, that is to say, '/r' '/n' '/r/n' can be expressed as line breaks, there will be a There will be a tuple to store the line breaks used in the file. However, although there are multiple modes of line breaks, they are uniformly replaced by /n in python. After the mode character, you can also add + b t two kinds of identification, respectively, indicate that you can read and write to the file at the same time and open the file in binary mode, text mode (the default).
buffering if 0 means no buffering; if 1 means "line buffering"; if it is a number greater than 1 means the size of the buffer, should be in bytes.

The file object has its own properties and methods. Let's start with the properties of file.

closed #Mark if the file is closed, rewritten from close().
encoding #file encoding
mode #Open mode
name #file name
newlines # The newline pattern used in the file is a tuple
softspace #boolean type, usually 0, said to be used for print

file's read and write methods:

([size]) #size is the length of the read in bytes
([size])
# Read a line, if size is defined, it is possible to return only a part of a line
([size])
# treats each line of the file as a member of a list and returns the list. it is actually implemented internally by calling readline() in a loop. If the size parameter is supplied, size is an indication of the total length of the read content, meaning that only a portion of the file may be read.
(str)
# Write str to a file, write() does not add a newline after str
(seq)
# Write the entire contents of the seq to the file. This function also just writes faithfully, without adding anything after each line.
file's other methods:

()
# Close files. python will automatically close a file when it is not in use, but this feature is not guaranteed and it is best to get in the habit of closing files yourself. If you operate on a file after it has been closed, a ValueError will be thrown.
()
# Write the contents of the buffer to the hard disk
()
# Returns a long integer "file label".
()
# Is the file a terminal device file (on unix systems)
()
# Returns the current position of the file action marker, originating at the beginning of the file
()
# Returns to the next line and displaces the file operation marker to the next line. When you use a file in a statement like for ... in file statements like this, it is the next() function that is called to implement traversal.
(offset[,whence])
# Move the file hit operation marker to the position of offset. The offset is usually calculated relative to the beginning of the file, and is usually a positive number. But if you provide whence parameter is not necessarily, whence can be 0 means from the beginning of the calculation, 1 means to the current position as the origin of the calculation. 2 means to the end of the file as the origin of the calculation. Note that if the file is opened in a or a+ mode, the file operation marker will automatically return to the end of the file each time a write operation is performed.
([size])
# Crop the file to a specified size, by default to the current file operation marker. If the size is larger than the file size, depending on the system, the file may be left unchanged, or the file may be padded with zeros to the appropriate size, or some random content may be added.

I hope that what I have described in this article will help you in your Python programming.