Python file operation 1,

Source: Internet
Author: User

Python file operation 1,
File Operation Process 1. open the file, get the file handle and assign it to a variable 2. Operate the file through the handle 3. close the file to open the file

Open () returns a file object. The basic syntax format is as follows:

Open (filename, mode)

 

1 ''' 2 data = open ("Nunber", encoding = "UTF-8 "). read () 3 the above method that directly assigns the content to the variable completely, so that this variable cannot be treated as an object. This method will only close the file after reading the file 4. Therefore, use the following method to set the file name, file size, the file length and pointer are encapsulated into an operable object, which is F8 first = f. read () 9 second = f. read () 10 11 print (first) 12 print ("__________ print the second time ____________") 13 print (second) 14 f. close ()

Execution result

July 5, 45678901234567890567890123456789016789012345678901278901234567890123890123456789012349012345678901234501234567890123456

File pointer:

Second re-assigned the value for the second time and did not print it. This is because after opening the file open, read and read one row and then read one row, the first read operation has moved the file pointer in the handle to the last position of the file. The file pointer is like the cursor in the word, so no content can be read during the second read operation. (File) Method
1 # f. read () read all without parameters. The parameter prints the specified number of bytes. 2 f = open ("Nunber", "r", encoding = "UTF-8") 3 my_read = f. read (21) 4 print (my_read) 5 6 # f. readline () does not add a parameter to read a row. The parameter prints the number of bytes for this row. 7 my_readline = f. readline (8) 8 print (my_readline) 9 10 # f. tell () shows the file pointer position 11 print (f. tell () 12 13 # f. seek () control file pointer position 14 f. seek (0) 15 print (f. tell () 16 17 # f. close () close file 18 f. close () 19 20 # f. readlines () read all rows by row without adding parameters, and save each row as an element as a class table. If the parameter is greater than 21 # bytes per row, the next row is saved as an element to the list. 22 22 23 f = open ("Nunber", encoding = "UTF-8") 24 my_readlines = f. readlines (40) 25 f. close () 26 print (my_readlines)

 

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.