Python file processing

Source: Internet
Author: User

1.window operating system with the encoding format is GBK,PYTHON3 encoding format is utf_8;

2. File Stream Processing

Get the file handle through open (), manipulate the operating system through the handle to read the contents from the document, and then close the handle when the document information is read, or it will always occupy the operating system.

R mode: read-only mode

F.read ()//Read all the contents of the document

F.readlines ()//Read the entire contents of the document

F.readline ()//read a line of content in the document, return a list

Print (F.readline (), end = ")//end =", remove blank lines after printing

F.readable ()//Determine if the file is readable

3. File Write operations

File write operation, in the case of a file already exists, will clear all the contents of the file, and then add content to the file, if the file does not exist, create a new file, and then write the contents to the file.

Write String

4. Append content actions to the end of the document: a mode

5. File read/write mode

Note: The file is stored on the hard disk, the file can not be modified by nature, the modified file is through the software, in memory to modify the file, and then save the modified content to the new file. If the modified content is saved in the original file, the previous content is overwritten.

The file write operation always starts at the position of the pointer and overwrites the content behind it.

6. Use the with operation to close the file without manually writing close ()

7. Open two files at the same time, read the information from one file, write this information to another file

Python file processing

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.