1. Open
After opening a file using open, remember to call the close () method of the file object. For example, you can use the try/finally statement to ensure that the file can be closed at last.
File_object = open('thefile.txt ')
Try:
All_the_text = file_object.read ()
Finally:
File_object.close ()
Note: The open statement cannot be placed in the try block, because when an exception occurs when the file is opened, the file object file_object cannot execute the close () method.
2. Read files
Read text files
Input = open ('data', 'R ')
# The second parameter defaults to R.
Input = open ('data ')
Read Binary files
Input = open ('data', 'rb ')
Read all content
File_object = open('thefile.txt ')
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
List_of_all_the_lines = file_object.readlines ()
If the file is a text file, you can 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 binary files
Output = open ('data', 'wb ')
Append a file
Output = open ('data', 'W + ')
Write Data
File_object = open('thefile.txt ', 'w ')
File_object.write (all_the_text)
File_object.close ()
Write multiple rows
File_object.writelines (list_of_text_strings)
Note that calling writelines to write multiple rows has a higher performance than using write for one-time writing.
When processing log files, we often encounter the following situation: the log files are huge and it is impossible to read the entire file into the memory at a time for processing, for example, to process a 2 GB log file on a machine with 2 GB physical memory, we may want to process only 200 MB of content at a time.
In python, the built-in file object directly provides a readlines (sizehint) function to accomplish this. The following code is used as an example:
File = open ('test. log', 'R') sizehint = 209715200 #200 mposition = 0 lines = file. readlines (sizehint) while not file. tell ()-position <0: Position = file. tell () lines = file. readlines (sizehint)
Every time you call the readlines (sizehint) function, it returns about 200 MB of data, and the returned data must be complete row data. In most cases, the number of bytes of the returned data is slightly larger than the value specified by sizehint (except when the last readlines (sizehint) function is called ). Generally, Python automatically adjusts the user-specified sizehint value to an integer multiple of the internal cache size.
File is a special type in Python. It is used to operate external files in Python programs. In python, everything is an object, and file is no exception. file has the methods and attributes of file. The following describes how to create a file object:
• File (name [, mode [, buffering])
The file () function is used to create a file object. A file object is called open (), which may be more vivid. They are built-in functions. Let's take a look at its parameters. Its parameters are all transmitted in the form of strings. Name is the name of the file.
Mode is the open mode. The optional value is R w a U, which represents the mode in which various line breaks are supported for read (default) writes. If you open a file in W or a mode, if the file does not exist, it is automatically created. In addition, when you open an existing file in W mode, the content of the original file will be cleared, because the operation mark of the initial file is at the beginning of the file, at this time, the write operation will undoubtedly erase the original content. For historical reasons, line breaks have different modes in different systems. For example, in UNIX, the line breaks are \ n, and in windows, the line breaks are \ r \ n ', opening a file in U mode supports all line breaks. That is to say, '\ r'' \ n' \ r \ n' indicates line breaks, there will be a tuple used to store the line breaks used in this file. However, although there are multiple line breaks, reading Python should be replaced by \ n. + B t can be added after the pattern character, indicating that the file can be read and written at the same time and the file can be opened in binary mode and text mode (default.
If buffering is set to 0, no buffer is performed. If it is set to 1, "Row buffer" is performed. If it is a number greater than 1, the buffer size is measured in bytes.
File objects have their own attributes and methods. Let's take a look at the file attributes.
• Closed # MARK whether the file is closed and rewritten by close ()
• Encoding # file encoding
• Mode # Open Mode
• Name # file name
• Newlines # The line feed mode used in the file is a tuple
• Softspace # boolean type, generally 0, is said to be used for print
File read/write method:
• F. Read ([size]) # size indicates the read length, in bytes.
• F. Readline ([size])
# Read a row. If the size is defined, only one part of the row may be returned.
• F. readlines ([size])
# Use each row of the file as a member of a list and return this list. In fact, it is implemented by calling Readline () cyclically. If the size parameter is provided, size indicates the total length of the read content, that is, it may be read only to a part of the file.
• F. Write (STR)
# Write STR to a file. Write () does not add a linefeed after Str.
• F. writelines (SEQ)
# Write All seq content to the file. This function is only faithfully written without adding anything to the end of each line.
Other File methods:
• F. Close ()
# Close the file. Python will automatically close the file after a file is not used, but this function is not guaranteed, it is best to develop your own habit of closing. If a file is closed and operated on it, valueerror is generated.
• F. Flush ()
# Write the buffer content to the hard disk
• F. fileno ()
# Return a long integer "file tag"
• F. isatty ()
# Whether the file is a terminal device file (in UNIX)
• F. Tell ()
# Return the current position of the file operation mark, starting with the file
• F. Next ()
# Return the next row and move the operation mark of the file to the next row. When a file is used in a statement such as for... in file, the next () function is called to implement traversal.
• F. Seek (offset [, whence])
# Move the file to the offset position by marking the operation. This offset is generally calculated relative to the beginning of the file, and is generally a positive number. However, if the whence parameter is provided, it is not necessary. If the whence parameter is 0, it indicates that the calculation starts from the beginning, and 1 indicates that the calculation is based on the current position. 2 indicates that the origin is the end of the file. Note that if the file is opened in a or a + mode, the Operation mark of the file is automatically returned to the end of the file each time the file is written.
• F. truncate ([size])
# Crop the file to a specified size. The default value is to crop it to the position marked by the current file operation. If the size is larger than the file size, the file may not be changed depending on the system, or 0 may be used to fill the file with the corresponding size, it may also be added with random content.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/adupt/archive/2009/08/11/4435615.aspx