Python file operations 1

Source: Internet
Author: User

Procedure for file Operation 1. Open the file, get the file handle, and assign a value to a variable 2. manipulate files by handle 3. Close File Open File

Open () Returns a file object with the following basic syntax format:

Open (filename, mode)

1 " "2data = open ("Nunber", encoding="Utf-8"). Read ()3 The above method, which directly assigns the content to a variable, cannot be treated as an object. This method only opens after the file is read4 close it now. So please use the following method5 " " 6f = open ("Nunber", encoding="Utf-8") 7 file handle (f): Encapsulates a file name, file size, file length, file pointer, etc. into an operable object, which is an operand of f8First =F.read ()9Second =F.read ()Ten   One print (first) APrint"__________ Print second time ____________") - print (second) -F.close ()

Execution results

1.2,345,234,567,345,7e+,120 1234567890123456__________ Print the second time the ____________ process has ended, exit code 0

About file Pointers:

Second re-assigned the second pass is not printed, this is because the open file, read read a line and then read a line to open, the first read has moved the file pointer within the handle to the last position of the file, the file pointer is like the cursor in Word, So I can't read anything the second time. (file) method
1 #f.read () No parameter read all, parameters print the specified number of bytes2f = open ("Nunber","R", encoding="Utf-8")3My_read = F.read (21)4 Print(My_read)5 6 #F.readline () reads a row without arguments, and the parameter prints the number of bytes in the specified line7My_readline = F.readline (8)8 Print(My_readline)9 Ten #F.tell () show file pointer location One Print(F.tell ()) A  - #f.seek () control file pointer position - f.seek (0) the Print(F.tell ()) -  - #f.close () Close file - f.close () +  - #f.readlines () reads all rows in rows without parameters and saves each row as an element as a class table. parameter is greater than the number of bytes per line + #The next line is stored as an element in the list.  A  atf = open ("Nunber", encoding="Utf-8") -My_readlines = F.readlines (40) - f.close () - Print(My_readlines)

Python file operations 1

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.