Python Basic Tutorial Learning notes---(6) file read and write

Source: Internet
Author: User
Tags print object

1. Open function Syntax:Python opens the file through the open function, creating a connection between the program and the file. Open function Syntax: Open (filename[,mode[,buffering]])where filename refers to the name of the file to be manipulated, consisting of the file name and path, which need to be quoted. mode is a parameter for operating modes, including read, write, append and so on. The buffering is a buffer parameter that controls the buffering of the file. When buffering=0 or false, I/O is unbuffered, all read and write operations are directly against the hard disk, if it is 1 or true,i/o is buffered, Python is memory to replace the hard disk, making the program run faster. Greater than 1 represents the size of the buffer, in bytes, 1 or any other negative number representing the default buffer size. only the filename parameter must be specified, the mode and buffering parameters are optional, and the default value for the mode parameter is ' R '.
  2, the parameter mode value: (1) Basic value:
R, W, and A are the basic modes of open files, which represent read-only, write-only (overwrite-write), append (Append-write). B, T, +, U, combined with the above three basic patterns. Note: text mode is used to open a text file, and binary mode users open binary files. (2) Common mode combination values: (3) illustrate:Now there is a file G:\file1.txt on the Windows System PC hard disk, it is shown. ①r mode:The cause of the error is that the ' \ ' contained in the path is recognized as an escape character, resulting in a file path error. You cannot print the contents of a print object F1 directly, and you need to read the content using the Read method before it can be printed. Although the mode parameter is not specified in the Open function above, the default value of mode ' R ' is used. effect, so when only the ' r ' parameter is used, it can be omitted. also, if you do not want to use two backslashes here, you can use the ' r+ string ' form:a string preceded by R indicates that the string is displayed as is, not escaped. The first ' R ' is to make the string appear as it is, and the second ' R ' is the open mode. ②w mode:because only the ' W ' is used, the program has only write permission on the file and no Read permission, so the Read method is used to make an error. after this step, through the Explorer to see the contents of the File1.txt file, as below, found that the inside has been emptied. this is because the program has not been disconnected from the file, so the appended content has not been saved, so you need to use the Close method to close the file (that is, disconnect the file from the program). Reopen the file to see that the file has been overwritten by new content updates. ③a mode:the reason for the error is that the ' a ' mode has only write permission and no Read permission. After you finish the write operation, but before you close the file, you can see that the File1.txt file is not updated. after you close the file, you can see that the new content is appended to the end of the file. if you want to wrap a newline, add a newline character to the string. ④r+ mode:The ' r+ ' mode is simultaneous read-write mode, but must specify the location of the write, overwriting the write by character, starting at the specified position. from the above two write operations can be seen, the first time through seek (0) specified from the first character to overwrite the write, the second time through Seek (5) from the 6th character to overwrite the write, not the overridden characters continue to persist. If you do not specify the location to start the write operation, it will be an error, as follows. for the specific role of the Seek () method, it will be discussed later. ⑤w+ mode:for the ' w+ ' mode, it simply adds the ability to read the file object on the ' W ' mode. In addition, it is important to note from the above example that once a file is assigned to a file object through the ' w+ ' mode, the file is emptied immediately, so the result of F10.read () here is an empty string. That is, using ' w ' or ' w+ ' mode, the contents of the original file are not overwritten when using the Write method, but are emptied immediately when the write permission is granted. ⑥a+ mode:The ' A + ' mode is more than ' a ' to increase the read file permissions, but from the above example can be seen, using the Read method, the write operation will be an error, and do not use the Read method of writing directly write operation is not error, and append write success.   3. Common file methods: (1) Basic introduction: (2) illustrate:existing file: G:\file2.txt, the file contents are as follows:The Read () method reads all the contents of the data in the entire file, appears as a string, and is explicitly displayed as ' \ n ' in the result for line breaks in the file. Before reading the read, the pointer is at the file header (position 0), and after reading all, the pointer is at the next bit at the end of the file. Returns the file location using the Tell () method, and the result is a long integer. The read (size) method specifies the number of bytes to read from the string. Initially the pointer is placed at the beginning of the file and, after reading, moves to the next bit of the read string. so here, using read (5) to read the first 5 characters, the pointer moves to the 6th character, which is the position numbered 5L. It then reads with read (7), starting with the 6th character, and so on. Use the ReadLine () method to read line by row, reading one line at a time, and then moving the pointer to the beginning of the next line. The ReadLines () method stores the results of the read as a list, and when it is finished, the pointer moves to the next bit at the end of the file. Each line in the original file is a return element in the list. Append write using ' A ' mode, after opening the file, the pointer still refers to the file header, after the write () method is written into the string, the pointer moves to the next bit of the end of the file. This example differs from the previous example to illustrate that when we want to append multiple lines to a file, we need to write out the newline character (\ n) explicitly in the appended string. the existing file File3.txt, the file content is as follows:Use the ReadLines () method to save the contents of the read file as a list. Then use the Writelines () method to append the list to the end of another file. Each element is called a row, because the starting position is the next bit (not the next line) at the end of the original file, so the first row appended does not go alone, but after the last line of the original file. The example above illustrates that the Writelines () method accepts a list and writes each element of the list to a file as a single line. Note that because there is a write () method, there is no WriteLine () method in Python.   

Python Basic Tutorial Learning notes---(6) file read and write

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.