Python Learning Notes (iv)-file operations

Source: Internet
Author: User

File read/write
"""
First, there are 3 ways to open the file
1, read R #如果打开的文件的时候没有指定模式, then the default is read
Read-write mode r+, as long as the R, the file does not exist, open will error
2, write W #w模式会清空原有文件内容
Read-write mode w+
Just dip the W, and he'll erase the file.
3. Append a
Append read/write
Second, the document pointer
F.seek (0) #seek移动指针的时候, only to read so, not to write well
When open the file pointer is in the initial position 1, when reading 4 bytes of content is the file pointer position in 4, then write, the file pointer from 4 to 8, so as long as the control of the file pointer, you can read it repeatedly!
The action file pointer can be through the Seek method:
Seek (offset, whence): Move the file pointer
Offset: Offsets, can be a negative number
Whence: Offset relative position

Offset relative position is seek_set, Seek_cur, seek_end in the OS module:
Os. Seek_set: Indicates the relative starting position of the file
Os. Seek_cur: Indicates the relative current location of the file
Os. Seek_end: Indicates the relative end position of the file
Three, the difference of python2\3
File () #python2里面打开文件还可以用file ()
Open () #python3
"""

f = open ('Ybq.txt','A +', encoding='Utf-8') F.seek (0)#seek to move the pointer, only to read so that it is not good to writePrint(F.tell ())#View the position of the current file pointerF.truncate ()#Empty file ContentsPrint(F.read ())#Read all the contents of the filePrint(F.readline ())#Read only one line of contentPrint(F.readlines ())#Read all the contents of the file and put the contents of each line in the file into a listF.write ('MPP'+'\ n')#Write StringPrint(F.read ()) names= ['YBQ','MPP','TLX','Ply','Yjy']f.writelines (names) St='YBQ,MPP,TLX'F.writelines (ST) F.write (ST)#only non-iterative objects, such as strings, can be writtenTu= ('SDF','SDFs','SDFSD') F.writelines (TU) d={'name':'xx'}f.writelines (d)#when writing, pass in an iterative object, such as list, Ganso, dictionaryf.close ()#open a file to write data to a file in a loopf = open ('a.txt','W') forIinchRange (3): F.write ('xx'+'\ n') F.close ()#ways to efficiently process filesFW = Open ('Ybq.txt', encoding='Utf-8') Count= 1#Loop the file object directly, that is, each line in the loop file forFinchfw:f= F.strip ()#remove empty, line breaksStu_lst = F.split (',')#Press ', ' to split into lists   Print(STU_LST)

File pointers
The experimental files data.txt as follows:
This file is Data.txt
Hello world!
My name is Mark.

File pointer initialization

 f = open ( " data.txt  " ,  " r+  "  )  print  F.tell () Span style= "COLOR: #008000" >#  f.write ( "  Nice to meet   " ) F.flush ()  #  flush () method is used to flush the buffer, the data in the buffer is immediately written to the file, and the buffer is emptied. It is not necessary to wait for the output buffer to be passive written.  print   F.tell ()  print   F.read () f.close ()  

Corresponding output:
Print F.tell ()
0
Print F.tell ()
16
Print F.read ()
A.txt
Hello world!
My name is Mark.

Description: Open the file in ' r+ ' mode, where the file pointer refers to 0
F.write (' Nice-to-meet you ') overwrites the original file contents, and the file pointer moves with
Print F.read () will only output the remaining content

f = open (R'test2.txt',' a+')print  F.tell () f.write('Nice-meetyou') F.flush ()print  F.tell ()print  f.read () f.close ( )

Corresponding output:
Print F.tell ()
52
Print F.tell ()
68
Print F.read ()

Note: Opened in ' A + ' mode, the file pointer is located at the end of the file, this example is 52
F.write (' Nice to meet ') will be added at the end of the original file content, with the file pointer moving to 68
Because with the write () function, the file pointer has moved to the end of the file, so print f.read () has no output

Python Learning Notes (iv)-file operations

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.