Python action file

Source: Internet
Author: User

1,python reading files

2,python Writing Files

3,python common ways to manipulate files

1,python read Normal file (file name is Text.txt)

f = open (' Test.txt ', ' r ') data = F.read () print data

F.close ()

  

This will enable the file to be read. By default the open file is read, so if you are reading the file, you can not write ' R ', of course, here can be written mode= ' R '.

Consider if the file is very large, such as a file of 1 g, if you use the Read method directly. This can be very expensive for server memory, which is not very good. So this is the time to consider Xreadlines. It is not in memory that loads content at once.

Only wait until you use the specific line to load the memory. This is similar to the principle of the previous range and xrange. The code changes as follows:

f = open (' Test.txt ', ' r ') data = F.read () print data

F.close ()

The open can actually be replaced with file, meaning the same. Found from the above code, after opening the file after a series of operations require F.close () to close the file. The reason for this shutdown is that if you do not close the file, the descriptor of the system is used, and the descriptor of the operating system is limited. Of course, the Python recycling mechanism is also dealing with this problem. But auto-closing is the best option after all. But each time f.close is more troublesome, so referencing with open, it will automatically help us execute the f.close () function.

With open (' Test.txt ', ' R ') as F:for line in F.xreadliens (): Print Line.strip ()

  

The above operations are for ordinary file operations, if you need to manipulate binary files, the operation of the file mode needs to change to ' RB ' or ' WB '. You can refer to the following examples:

2,python Writing Files

With open (' Test.txt ', ' W ') as F:f.write (' This is Test ')

  

This completes the basic file write operation, but our pattern here is W, which means that the original content has been emptied, and the content of the contents of the empty file under the premise of the new additions.

If you do not want the contents of the original file to be emptied, then you need to change the mode to append mode, which is ' a '.

With open (' Test.txt ', ' a ') as F:f.write (' This is Test ')

  

3,python common ways to manipulate files

Read mode:

Read () reads the contents all at once

ReadLine () reads a line of files, such as opening a file, executing f.readline () means reading the first line of the file, re-manipulating, reading the second line of the file

ReadLines () reads all the lines of the file, returning as a list

Xreadlines () reads the rows of the file, returns the object, which is the generator, which needs to be traversed with a for loop

Next () reads the next line of the file

Seek () specifies that the file pointer jumps to that position, which is typically used to let the pointer jump to the beginning of the start line. namely F.seek (0)

Tell () returns where the pointer is located

Write mode:

Wirte () writes content to a file at once

The Writelines () file writes a list of sequence strings and adds a newline character to each line if a line break is required

Flush () Refreshes the changes to disk, and in general, you f.write write to the content here. But when you don't have f.close, the content you write to the file is temporarily cached in memory. Unless you f.close the file to disk, or you use the F.flush () command to flush the content to disk

Close () closes the file

Python action file

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.