Python is recommended for reading and writing files and file objects

  • 2020-05-10 18:28:53
  • OfStack

1.open

Remember to call the close() method of the file object after opening the file with open. For example, you can use the try/finally statement to make sure you close the file at the end.

file_object = open('thefile.txt')
try:
        all_the_text = file_object.read( )
finally:
        file_object.close( )
Note: you cannot put the open statement in the try block, because the file object file_object cannot execute the close() method when an exception occurs when opening a file.

2. Read the file

Read text file
input = open('data', 'r')
The second parameter defaults to r
input = open('data')
 

Read the base 2 file
input = open('data', 'rb')

Read everything
file_object = open('thefile.txt')
try:
        all_the_text = file_object.read( )
finally:
        file_object.close( )

Read fixed byte
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
list_of_all_the_lines = file_object.readlines( )
If the file is a text file, you can also directly traverse the file object to get each line:

for line in file_object:
        process line

3. Write files

Write a text file
output = open('data', 'w')

Write a base 2 file
output = open('data', 'wb')

Append file
output = open('data', 'w+')

Write the data
file_object = open('thefile.txt', 'w')
file_object.write(all_the_text)
file_object.close( )

Write more lines

file_object.writelines(list_of_text_strings)

Note that calling writelines to write multiple lines would have a higher performance than using write1 for a secondary write.

When dealing with log files, we often encounter the situation that the log files are so large that it is impossible to read the whole file into memory for processing at one time. For example, if we need to process one log file of 2GB on a machine with 2GB physical memory, we may want to process only the contents of 200MB at a time.

In Python, the built-in File object directly provides an readlines(sizehint) function to do this. The following code is an example:

file = open('test.log', 'r')sizehint = 209715200     # 200Mposition = 0lines = file.readlines(sizehint)while not file.tell() - position < 0:             position = file.tell()             lines = file.readlines(sizehint)

Each call to the readlines(sizehint) function returns about 200MB of data, and it must always return a full row of data. In most cases, the number of bytes returned is slightly larger than the value specified by sizehint (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 size of the internal cache.

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

•file(name[, mode[, buffering]])

The file() function is used to create an file object. It has a 1 called open(), which is probably more like 1. They are built-in functions. Let's look at the parameters. The arguments are passed as strings. name is the name of the file.

mode is the open mode, with an optional value of r w a U, which stands for read (default) write add mode that supports various newline characters. If the file is opened in w or a mode, it is automatically created if it does not exist. In addition, when an existing file is opened in w mode, the contents of the original file will be emptied, because the mark for the operation of the file starting from 1 is at the beginning of the file. At this time, the write operation will undoubtedly erase the original content. For historical reasons, newline characters have different modes in different systems. For example, in unix, it is 1 \n, and in windows, it is '\ r\n'. To open the file in U mode is to support all newline modes, so it means '\ r' \n' \r\n'. However, although there are many patterns for line feeds, when I read python I used \n instead. After the pattern character, you can add + b t, which means you can read and write to the file at the same time and open the file in base 2 mode and text mode (the default), respectively.

buffering if 0 means no buffering; If 1 means "row buffering"; If a number greater than 1 represents the size of the buffer, it should be in bytes.

The file object has its own properties and methods. Let's look at the properties of file first.

The & # 8226; closed # marks whether the file has been closed and is overwritten by close()
The & # 8226; encoding # file encoding
The & # 8226; mode # opens mode
The & # 8226; name # file name
The & # 8226; The newline mode used in the newlines # file is 1 tuple
The & # 8226; Type softspace #boolean, 0 as 1, said to be used for print

Read and write methods for file:

The & # 8226; F.read ([size]) #size is the length read, in byte

•F.readline([size])

# reads line 1, and if size is defined, it is possible to return only part 1 of line 1

•F.readlines([size])

Take each line of the file as a member of an list and return this list. It's actually done internally by calling readline() in a loop. If the size parameter is provided, size is the total length of the read, which means that you may read only part 1 of the file.

•F.write(str)

# writes str to a file. write() does not add a newline after str

•F.writelines(seq)

Write all of seq to the file. This function also just writes faithfully and does not add anything after each line.

Other methods of file:

•F.close()

Close the file. python will automatically close a file when it is not in use, but this is not guaranteed, so it is best to make a habit of closing it yourself. ValueError is generated if a file is operated on after closing

•F.flush()

Writes the contents of the buffer to the hard disk

•F.fileno()

Return 1 long integer "file label"

•F.isatty()

Is the # file a terminal device file (unix system)

•F.tell()

# returns the current location of the file action tag, starting at the beginning of the file at the origin

•F.next()

# returns the next line and shifts the file action tag to the next line. Use 1 file for for... Statements like in file are traversed by calling the next() function.

•F.seek(offset[,whence])

Move the file action tag to offset. This offset1 is calculated relative to the beginning of the file, and 1 is usually positive. However, if the parameter whence is provided, it will not be fixed. whence can be 0 to calculate from scratch, and 1 to calculate from the current position as the origin. 2 represents the calculation with the end of the file as the origin. Note that if the file is opened in a or a+ mode, the file action flag is automatically returned to the end of the file each time a write is made.

•F.truncate([size])

Size the file to the specified size, by default, to the location of the current file action flag. If size is larger than the size of the file, depending on the system, the file may not be changed, the file may be filled to the appropriate size with 0, or it may be added with 1 random content.


Related articles: