Python file Operation example-to be perfected

Source: Internet
Author: User

#w写模式, R read mode, a append, r+ read/write, w+ write, a + append read, RB two-format read file, WB write two-level file
‘‘‘

f = open (' Yesterday ', ' r+ ', encoding= ' utf-8 ')
f = open (' Yesterday ', ' w+ ', encoding= ' utf-8 ')
f = open (' Yesterday ', ' RB ') #文件句柄, binary files, two cases use: Network transmission, read binary files, avoid damaging files
f = open (' Yesterday ', ' WB ') #写为二进制文件
#data = F.read ()
#print (data)
F.write (' \ n i love Beijing Tian ' an gate ... \ n ')
F.write (' Rising Sun on Tiananmen Square ')
data = F.read ()
Print (data)
F.close ()
‘‘‘
‘‘‘‘
f = open (' Yesterday ', ' r+ ', encoding= ' utf-8 ')
Print (F.readline ())
Print (F.readline ())
Print (F.readline ())
Print (F.tell ())
F.write ('-------------------------------------------------')

Print (F.readline ())
F.close ()
‘‘‘
f = open (' Yesterday ', ' WB ')

f = open (' Yesterday ', ' RB ')
#f. Write ("Hello binary\n". Encode ()) #存二级制则需要转换为二级制, or error
Print (F.readline ())
F.close ()


‘‘‘
Print (F.tell ()) #打印当前的位置
Print (F.readline ())
Print (F.readline ())
Print (F.readline ())
Print (F.tell ())
F.seek (#回到的地方)
Print (F.readline ())
Print (f.encoding) #返回编码格式
Print (F.fileno ()) #文件句柄编号
Print (F.seekable ()) #判断光标是否可以移动
Print (F.readable ()) #文件是否可读
Print (F.writable ()) #判断文件是否可以写
‘‘‘

‘‘‘
F.write (' Hello 1\n ')
Print (F.buffer)
Print (F.flush ()) #刷新到硬盘
F.seek (10)
F.truncate (#不写就是清空), truncate from the beginning
‘‘‘

#for I in range (5):
# Print (F.readline ())
#print (F.readline ())
#for line in F.readline ():
#print (F.readlines ())
#low
‘‘‘
For Index,line in Enumerate (F.readlines ()): #f. ReadLines () Suitable for small files
If index==9:
Print ('-----------I'm line 9th-------------')
Continue
Print (Line.strip ())
‘‘‘
#high Bige is read in a row, and the memory remains the same
‘‘‘‘
Count = 0
For line in F:
If Count ==9:
Print ('-----------I'm line 9th-------------')
Count + = 1
Continue
Print (Line.strip ())
Count +=1
‘‘‘


Python file Operation example-to be perfected

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.