Python Read and write files

Source: Internet
Author: User

Python Read and write files
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 ')
#第二个参数默认为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 (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 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 # 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 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 to 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 patterns, 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, also say ' \ r ' \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ A tuple is used to store the newline characters used in this file. However, although there are multiple modes of line break, read the unified use \ n instead of 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.

Code #! /usr/bin/python
ImportOs,sys

Try:
Fsock=Open"d:/svntest/test.py","R")
ExceptIOError:
Print "The file don ' t exist, please double check!"
Exit ()
Print ‘The file mode is‘, Fsock.mode
Print ‘The file name is‘, Fsock.name
P=Fsock.tell ()
Print ‘The postion is%d‘ %P
Fsock.close ()

#Read file
Fsock=Open"d:/svntest/test.py","R")
Alllines=Fsock.readlines ()
#Method 1
ForEachlineInchFsock:
PrintEachline

#Method 2
Print ‘Star‘+‘=‘*30
ForEachlineInchAlllines:
PrintEachline
Print ‘End‘+‘=‘*30
Fsock.close ()

#Write this file
Fsock=Open"d:/svntest/test.py","A")
Fsock.write ("""
#Line 1 Just for test purpose
#Line 2 Just for test purpose
#Line 3 Just for test purpose""")
Fsock.close ()


#check the file Status
s1 = fsock.closed
Span style= "color: #0000ff;" >if== S1:
Span style= "color: #0000ff;" >print the file is Closed '
else:
print the file donot close "

Transferred from: http://blog.csdn.net/adupt/archive/2009/08/11/4435615.aspx

Python Read and write files

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.