Python file read and write summary

Source: Internet
Author: User

Read the file

Open a file with the open () method (open () to return a file object, which is iterative):

>>> f = open ('test.txt'r')

R represents a text file, and RB is a binary file. (The default value for this mode parameter is R)

If the file does not exist, the open() function throws an IOError error and gives the error code and detailed information to tell you that the file does not exist:

>>> F=open ('test.txt'R') Traceback (most Recent call last):  '<stdin>' in <module>  or ' Test.txt '

The file must be closed after it is used because the file object consumes the resources of the operating system, and the number of files that the operating system can open at the same time is limited

>>> F.close ()

Since the file is read and written, it is possible to generate it, and IOError in the event of an error, f.close() it is not called. Therefore, in order to ensure that the file is closed correctly, whether or not it is an error, we can use it try ... finally to implement:

Try :     = Open ('/path/to/file'r'    )Print  (F.read ())finally:    if  f:        f.close ()

But it's so tedious to write every time, so Python introduces a with statement to help us invoke the close() method automatically:

With open ('/path/to/file'R') as F:    Print (F.read ())

The Python file object provides three "read" Methods: Read (), ReadLine (), and ReadLines (). Each method can accept a variable to limit the amount of data that is read each time.

    • Read () reads the entire file every time, and it is typically used to place the contents of a file into a string variable. If the file is larger than the available memory, for insurance purposes, you can call the read(size) method repeatedly, reading the contents of the size byte up to one time.
    • The difference between readlines () is that the latter reads the entire file one at a time, like. Read (): ReadLines () automatically parses the contents of the file into a list of rows that can be used by Python for ... in ... Structure for processing.
    • ReadLine () reads only one line at a time, usually much slower than readlines (). You should use ReadLine () only if you do not have enough memory to read the entire file at once.

Note : There are three ways to read the ' \ n ' at the end of each line, and it will not remove the ' \ n ' By default, we need to remove it manually.

IN[2]: With open ('Test1.txt','R') as F1:list1=f1.readlines () in[3]: list1out[3]: ['111\n','222\n','333\n','444\n','555\n','666\n']

Remove ' \ n '

IN[4]: With open ('Test1.txt','R') as F1:list1=F1.readlines () forIinchRange (0, Len (list1)): List1[i]= List1[i].rstrip ('\ n') in[5]: list1out[5]: ['111','222','333','444','555','666']

Read () and ReadLine () are also reading ' \ n ', but print can be displayed normally (because ' \ n ' in print is considered to be a newline meaning)

IN[7]: With open ('Test1.txt','R') as F1:list1=f1.read () in[8]: list1out[8]:'111\n222\n333\n444\n555\n666\n'in[9]:Print(List1)111222333444555666in[]: With open ('Test1.txt','R') as F1:list1=f1.readline () in[11]: list1out[11]:'111\n'in[12]:Print(List1)111

An example of a Python interview question:

There are two files, each with many line IP addresses, to find the same IP address in two files:

# Coding:utf-8
Import bisect

With open (' Test1.txt ', ' R ') as F1:
List1 = F1.readlines ()
For I in range (0, Len (list1)):
List1[i] = List1[i].strip (' \ n ')
With open (' Test2.txt ', ' R ') as F2:
List2 = F2.readlines ()
For I in range (0, Len (list2)):
List2[i] = List2[i].strip (' \ n ')

List2.sort ()
length_2 = Len (list2)
Same_data = []
For I in List1:
pos = Bisect.bisect_left (List2, i)
If Pos < Len (list2) and list2[pos] = = I:
Same_data.append (i)
Same_data = List (set (Same_data))
Print (Same_data)

The key point is: (1) using the "\ n" (3) with the end of the line with (2) to improve the efficiency of the algorithm. (4) Use set to quickly remove weight.

Write a file

Writing and reading files are the same, the only difference being that when a function is called, an identifier is passed in or a write open() - ‘w‘ ‘wb‘ in file or a binary file is written:

>>> f = open ('test.txt'w') # if ' WB ' means to write a binary file >>> f.write ('Hello, world! ' )>>> f.close ()

Note : The ' W ' mode is Jiangzi: If you do not have this file, create one, and if so, you will first empty the contents of the original file and then write something new. Therefore, if you do not want to empty the original content, but directly after the new content, you use the ' a ' mode.

We can write() write the file over and over again, but be sure to call f.close() to close the file. When we write a file, the operating system often does not immediately write the data to disk, but instead put it in memory cache, and then write slowly when idle. Only when the method is invoked close() does the operating system guarantee that all data that is not written is written to disk. The consequence of forgetting the call is that the close() data may have only been written to the disk, and the remainder has been lost. So, it is safe to use with statements:

With open ('test.txt'w') as F:    F.write ( ' Hello, world!. ')

The Python file object provides two write methods: Write () and Writelines ().

    • The write () method corresponds to the read (), ReadLine () method, which writes a string to the file.
    • The Writelines () method corresponds to the ReadLines () method and is also an action on the list . It takes a list of strings as arguments, writes them to a file, the line breaks are not automatically added, and therefore, you need to explicitly add a newline character.
F1 = open ('Test1.txt','W') F1.writelines (["1","2","3"])#at this point the contents of Test1.txt are: 123F1= Open ('Test1.txt','W') F1.writelines (["1\n","2\n","3\n"])#at this point the contents of Test1.txt are:#1#2#3

about the mode parameter of open () :

' R ': Read

' W ': Write

' A ': Append

' r+ ' = = R+w (can be read and written, if the file does not exist on the error (IOERROR))

' w+ ' = = W+r (can be read and written, and the file will be created if it does not exist)

' A + ' ==a+r (can be appended to write, the file is created if it does not exist)

Corresponding, if it is a binary file, just add a B is ok:

' RB ' WB ' AB ' rb+ ' wb+ ' ab+ '

File_obj.seek (offset,whence=0)

The File_obj.seek (offset,whence=0) method is used to move a file pointer in a file. Offset indicates how much offset. The optional parameter, whence, indicates where to start the offset, the default is 0 for the beginning of the file, 1 for the current position, and 2 for the file trailer. Example:

f = open ("test1.txt""a+")print( F.read ()) f.write ('1') f.seek (0, 0)#  Move the file pointer from the end to the beginning, Without this sentence read () will not be reading the right thing print(F.read ()) F.close ()

Note : This file pointer change only works on ' R ', does not work on ' w ' and ' a ', and if it is ' W ', then write () is always written from the beginning (overwriting the contents of the corresponding position at the back), and write () is always appended from the last start.

Character encoding

To read a text file that is not UTF-8 encoded, you need to pass in parameters to the open() function encoding , for example, to read the GBK encoded file:

>>> f = open ('test.txt'r', encoding='  GBK')>>> f.read ()' test '

If you encounter some code that is not canonical, you may encounter UnicodeDecodeError a number of characters that are illegally encoded in a text file that may be mixed. In this case, the open() function also receives a errors parameter that indicates what to do if a coding error is encountered. The simplest way is to ignore it directly:

>>> f = open ('test.txt'r', encoding='  GBK', errors='ignore')

Python file read and write summary

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.