Python File processing learning: A

Source: Internet
Author: User
1. File Write (... ' W ')
f1 = file(‘/test/test.txt‘,‘w‘)    #‘w‘ 开启写操作f1.write(‘the line is first.\n‘)  #write,写入f1.flush()             #刷到硬盘
2.file Reading (... ' R ')
f1 = file(‘/test/test.txt‘,‘w‘)   #‘r‘ 开启读操作f1.read()         #read ,读文件
3.file Append (... ' a ')
f1 = file(‘/test/test1.txt‘,‘a‘) #‘a‘ 开启追加操作f1.write(‘the line is second.\n‘)f1.flush()
4.file exit cache (Close)
f1.close()
5.file read/write mode (... ' r+ ') #不常用
f1 = file(‘test1.txt‘,‘r+‘)f1.write(‘the change for file test.\n‘)f1.flush()#这时查看test1.txt发现文件第二行被替换了,读写模式
6.file Write-read mode (... ' w+ ') #不常用
#自己测试
7.file Cross-platform binary conversion (.... RB or WB)
#类似windows上某些文件传到linux上,读行时,发现很多不一样的字符串,我们经常在linux上使用dos2unix来处理该文件,在python中 rb/wb读转换f1 = file(‘test1.txt‘,‘rb‘)

Python File processing learning: A

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.