Python Read file

Source: Internet
Author: User

1.open

always 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 ensure that the file is 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 the file object File_object cannot execute the close () method when an exception is opened to the file.

2. Read the file Read text file 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 byte file_object = open ('abinfile', ' RB ' )
try :
while True:
Chunk = file_object.read ( )
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 traverse the file object directly to get each line:

for line in File_object:
Process Line

 

3. Writing Files Write a text file output = open ('data', 'w ' )

 

write a binary file output = open ('data', 'WB ' )

 

Append Write File output = open ('data', 'w+ ' )

 

Write Data file_object = open ('thefile.txt', ' W ' )
File_object.write (All_the_text)
File_object.close ()

 

Write Multiple lines file_object.writelines (list_of_text_strings)


Note that calling Writelines writes multiple rows at a higher performance than using write one-time writes.

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

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


each call to the ReadLines (Sizehint) function returns approximately 200MB of data, and the return is necessarily the complete row data, in most cases the number of bytes returned is slightly larger than the value specified by Sizehint (except for the last call ReadLines (sizehint) function. Typically, Python automatically adjusts the value of the user-specified 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 a Python program. In Python everything is an object, file is no exception, file has the method and properties of file. Let's look at how to create a file object:

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

The file () function is used to create a file object that has a name of open () and may be more image-built functions. Take a look at its parameters. Its parameters are passed as a string. Name is the name of the file.
Mode is open, and the optional value is R w a U, which represents read (default) write to add patterns that support various line breaks. If you open the file in W or a mode, it will be created automatically if the file does not exist. In addition, when you open an existing file in W mode, the contents of the original file will be emptied, because the operation of the first file is marked at the beginning of the file, and the writing operation will undoubtedly erase the original content. For historical reasons, newline characters in different systems have different modes, such as in Unix is A/n, and in Windows is '/r/n ', in the U-mode to open the file, is to support all the newline mode, it is said that '/R '/n '/'/r/n ' can represent line break, A tuple is used to store the newline characters used in this file. However, although there are multiple modes for line breaks, read the unified use of/n instead in Python. After the pattern character, you can also add a + B t these two identities, respectively, can be read and write to the file and in binary mode, text mode (the default) to open the file.
Buffering if 0 means no buffering, if 1 means "row buffer", or if a number greater than 1 indicates the size of the buffer, it should be in bytes.

The file object has its own properties and methods. First look at the properties of file.

    • closed #标记文件是否已经关闭, rewritten by close ()
    • encoding #文件编码
    • mode #打开模式
    • name #文件名
    • newlines #文件中用到的换行模式, is a tuple
    • softspace #boolean型, typically 0, is said to be used for print


file Read and Write methods:

    • f.read ([size]) #size为读取的长度, in bytes
    • f.readline ([size])
      #读一行, if size is defined, it is possible to return only part of a row
    • f.readlines ([size])
      #把文件每一行作为一个list的一个成员 and return to this list. In fact, its internal is through the Loop call ReadLine () to achieve. If you provide a size parameter, size is the total length of the read content, which means that it may be read only to a portion of the file.
    • F.write (str)
      #把str写到文件中, write () does not add a newline character after Str
    • f.writelines (seq)
      #把seq的内容全部写到文件中. This function simply writes faithfully and does not add anything behind each line.

Other methods of file:

  • F.close ()
    #关闭文件. Python will automatically close files after a file is not used, but this feature is not guaranteed and it is best to develop a habit of shutting them down. If a file is closed and then manipulated, it generates ValueError
  • F.flush ()
    #把缓冲区的内容写入硬盘
  • F.fileno ()
    #返回一个长整型的 "File Label"
  • F.isatty ()
    #文件是否是一个终端设备文件 (on UNIX systems)
  • F.tell ()
    #返回文件操作标记的当前位置, starting with the origin of the file
  • F.next ()
    #返回下一行 and shifts the file action marker to the next line. When a file is used for a statement such as for ... in file, it is called the next () function to implement the traversal.
  • F.seek (Offset[,whence])
    #将文件打操作标记移到offset的位置. 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, whence can be calculated from scratch for 0, and 1 for the current position as its origin. 2 means that the end of the file is calculated as the origin. Note that if the file is opened in a or a + mode, the file action tag is automatically returned to the end of the file each time the write operation is made.
  • f.truncate ([size])
    #把文件裁成规定的大小, the default is to crop to the location of the current file action tag. If the size of the file is larger, depending on the system may not change the file, it may be 0 files to the corresponding size, it may be some random content to add.

Python reads a file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.