A detailed introduction to Python's file handling

Source: Internet
Author: User
Tags file handling
In Python, if you want to work on a file in a hard disk, it can be divided into three steps, the process is as follows:

Opens a file handle with the Open function and assigns a value to a variable.

The specified file is manipulated by the corresponding file handle.

When the operation is complete, the file is closed and the contents of the file are written to disk.

The Open function is used in the following way.

Open (' File path ', mode= ' Open file Mode ', encoding= ' file Encoding method ')

File path: This file path can be an absolute path, or a relative path, the relative path in Python only need to write the file name, if the Python program and the need to open the file if in a directory, the direct use of the relative path can be.

Attention! If you want to use the Open function, the file path is the necessary parameter, you can not pass!

Open File Mode: The Open function has an unnecessary parameter, which is mode, which is used to define the open mode of the file, and if you do not specify the file open mode, the file is opened by default using the R (read-only) mode.

Common files that are provided in Python are opened as follows:

' R ' opens the file in read-only mode, using R (read-only mode) to open the file, the file is read-only and cannot be written. (the file does not exist, then an exception is thrown)

' W ' opens the file in write-only mode, uses W (write-only mode) to open the file, the file can only be written and cannot be read, but there is a point to pay special attention to!!!!! A file that originally has content, once opened with W, then the contents of this file will be emptied!! (As for the reason, this article will be supplemented later.) (If you don't want the original contents of the file to be emptied!!) Then don't use W mode!!!!!!! ) (When the file does not exist, a file is created, and if the file exists, the contents of the file are emptied first)

The ' a ' append mode, also a write-only mode, is used to append content at the end of the Ask price, and the content written will be appended to the bottom of the file

' B ' binary mode, use binary way to open file, note! This ' B ' (binary mode) is to be used in combination with (R,w,a) three modes. (This mode is recommended across platforms across operating systems)

' r+ ' readable writable (in this mode, although readable can be written, but when writing it is important to note that the seek pointer is still in the head of the file, if you do not adjust the position of the seek pointer directly to start writing, will directly begin to overwrite the previous written content (starting from the file head), so that when using r+ mode, Be sure to note the position of the seek pointer in the file where!! Otherwise, the original content will be overwritten. )

' w+ ' can be written and readable (this mode is not used in general, it also empties files directly)

Append to the end of ' A + ', writable and readable

I. Common methods for manipulating file objects

Read the file:

Readable () is used to determine if the file is readable and returns true if it is readable, otherwise false.

ReadLine () reads a single line of the file at a time, returning a string type.

Read () reads all the contents of the file at once, returning an entire string.

ReadLines () reads all the contents of the file and adds each line of the file to a list, and each line of the file is used as an element in the list.

Write file:

Writable (): Determines whether the file is writable, if writable, returns True, otherwise returns false.

Write (): write to the file, only if the file is in writable mode, you can use this method, depending on where the file is written, depending on the open mode of the file (r+ or a + or w+), and depending on where the current seek pointer points to the file. (In addition, you can use the Write method to write content to the inside of a file without a newline character, you need to add a newline character manually, otherwise all the content will stick together.) )

Example: F1.write (' hello!\n ') #\n is a line break.

Writelines (): Similar to Wirte, are written to the inside of the file, and write is different, Writelines is to use the form of a list to write to the inside of the file, using the Writelines method, Python will cycle the list, Each element in the list is written to the file.

Attention! When you use Writelines to write content inside a file, it is also without line breaks, and if you add a newline character to the end of each element, each element in the list is a row in the file.

Attention!! The content written in the file can only be a string, not another type!! Otherwise you will throw an exception, even if you want to write a number, then this number must be converted to a string type!!!

Other operations:

Close () Closes the file, and when the file is read or finished, be sure to close the file using Close! (except with the with syntax, because a file is opened with the WITH keyword, and the file is closed automatically when the file is finished).

Does not close after reading the file, the program will always occupy system resources.

After writing the file does not close, will cause the contents of the memory will not be synchronized to the hard disk in a timely manner, if you want to completely write the content to the hard disk, or use close to shut down the file, otherwise, use the Flush method to force the memory of the data to the hard disk.

Flush () The data that is not written to the hard disk in memory is strongly brushed to the hard disk.

Encoding: Displays the encoding of the file open (this method is not available in Python2, which can be used in Python3. )

Tell (): The position of the current seek pointer can be obtained.

Seek (pointer position, mode), in bytes, moves the seek pointer in the position of the file.

There are three modes of operation of the Seek pointer in Pyrhon, and here is a detailed description of the three modes:

File.seek (n,0) #n代表了指针的位置, the following number 0 represents the sequence number of the pattern.

File.seek (n,0): (mode 0) mode 0 represents, absolute position, N is a few, will move the pointer to start from the beginning of the file the first byte of arithmetic. (When you use Seek, the default mode is 0 if you do not specify a pattern.) )

For example, File.seek (3,0) moves the pointer to the three-byte position starting at the beginning of the file (No. 0 byte).

# before using the pointer

F1 = open (' SEASONS.LRC ', mode= ' R ')

Print F1.readline ()

>>> あゆみ-Seasons, Saitama

# Here's the 0 pattern, moving the pointer to the 3rd byte of the file, moving the pointer to the 3rd byte, and reading the file, starting at the back of the pointer.

F1 = open (' Seasons.txt ', mode= ' R ')

Print F1.tell () #显示一下seek指针当前的位置

>>>0 # (position is 0, representing the pointer at the beginning of the file)

F1.seek (3,0) # (moves the pointer to the third byte, using 0 mode, absolute position)

Print F1.tell () # Look at the pointer position again, and you can verify that the position of the pointer is actually moved to the third byte.

>>>3

Print F1.readline () # reads a row after the position of the current pointer.

>>> Kawasaki あゆみ-Seasons

At this point, someone may ask, read the file from the back of the pointer to read the right, but obviously the pointer moved to three bytes, ah, why did you skip a character?

This needs to understand what is the character and Byte, must clear this concept!! In the Utf-8 character encoding, a Chinese character will occupy three bytes, the original content of the first line is "Hama あゆみ-Seasons", because a Chinese character accounted for three bytes, so the pointer was moved backwards three bytes, exactly a Chinese character position, the pointer was moved to "bang" behind, read the file, Read from behind the word, so the show is "Kawasaki あゆみ-Seasons". (A Japanese character also accounts for three bytes in the Utf-8 characters encoding.) )

In the following example, this example can understand the meaning of "absolute position".

F1 = open (' SEASONS.LRC ', mode= ' R ')

Print F1.tell () #打开文件后, the default position of the pointer is 0.

>>>0

F1.seek (3,0) # will refer to the position of 3 bytes moved to the file.

Print F1.tell ()

>>>3

F1.seek (3,0)

Print F1.tell ()

>>>3

File.seek (n,1): (Mode 1) mode 1, relative position, n means that the pointer moves backwards several bytes in the current position.

If you feel that I am not very easy to understand, I would like to look at the following example, I understand.

F1 = open (' SEASONS.LRC ', mode= ' R ')

Print F1.tell () #打开文件后, the default position of the pointer is 0.

>>>0

F1.seek (3,1) # Moves the pointer backwards by 3-byte position.

Print F1.tell ()

The >>>3 # pointer moves to the position of the 3rd byte.

F1.seek (3,1) # Here's the point, move the pointer backwards by 3 bytes (in this place you can compare the difference between Pattern 1 and pattern 0. )

Print F1.tell ()

The position of the >>>6 # pointer is on the 6th byte, which means that each move of the 1 pattern does not start at the beginning of the file, but moves backwards based on where the pointer was last. (This is what "relative position" means.) )

If you still can't understand it, look at the following supplement.

Finally, to add:

F1.seek (3,0) is the position at which the pointer moves to the 3rd byte of a file. (absolute position)

F1.seek (3,1) is a position that moves the pointer backward 3 bytes from the current position. (relative position)

File.seek (-n,2): Moves to the beginning of the file, starting at the very end of the file, in absolute position. (When using 2 mode, it is important to note that the pointer moves in a negative position only because it moves forward from the very end!) )

Here is an example:

# Open a file and use the Read method to read from the beginning to the end of the file, and when the file is read, the pointer will naturally move back to the bottom of the file.

F1 = open (' Seasons.txt ', mode= ' R ')

Print F1.tell ()

>>>0

F1.read ()

Print F1.tell ()

>>>756

Now you know by the method above that the first byte of the file is the number of bytes in the file.

Here's a test of whether the 2-mode feature of the Seek method, as previously stated, moves from the very end of the file to the beginning of the file.

F1 = open (' Seasons.txt ', mode= ' R ')

Print F1.tell ()

>>>0

F1.seek ( -1,2) #使用seek的2模式, move the pointer forward 1 bytes from the end of the file

Print F1.tell ()

>>>755

# The end of the file is 756, move forward a byte is 755, get the effect we want to get.

In fact, the 2 mode of the Seek method is still more useful, the following is a detail of the 2 mode of the Seek method.

You can use the 2 mode of the Seek method when you get the penultimate line of the file to line N.

At this point may be asked, take out the last line of the file, it is not very simple operation, as long as the use of the ReadLines method to read the file, and then take the last element, you can take the last line, this method is not easier?

Like this.

F1 = open (' Seasons.txt ', mode= ' R ')

Print F1.readlines () [-1]

>>>そして Ho を?つけるだろう

The last line of the file is actually taken out, but, there is no thought, this use readlines read the nature of the file, is to read every line of the file into memory, if the file is particularly large, for example, 10 g,100g, large to the current memory can not fit, if this is the case, This method is not applicable.

The following method is especially useful for large files.

F1 = open (' Seasons.txt ', mode= ' R ')

For I in F1: #直接for循环文件句柄, does not read the file to memory at once, but reads one line from the file.

Line_bytes =-36

While True:

F1.seek (line_bytes,2)

data = F1.readlines ()

If Len (data) > 1:

Print Data[-1]

Break

Else

Line_bytes * 2

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.