python--file read/write

Source: Internet
Author: User

f = open (' xx ', ' A + ', encoding= ' utf-8 ')

#文件打开有3种方式

R r+

W w+

A +
# Read R #如果打开的文件的时候没有指定模式, then default is read
# Read and write mode r+, as long as the R, the file does not exist, open will error
# Write W w mode will empty the original file contents
# read-write mode w+
#只要沾上w, the contents of the file will be emptied.
#追加 A
# Append read/write

F.seek (0) #seek移动指针的时候, only to read so, not to write well

F.truncate () #清空文件内容

F.read () #读取文件里面所有的内容, returns a string
F.readline () #只读一行的内容, the returned string
F.readlines () #读取文件里面所有的内容, the return is a list
#list里面每个元素是每行的数据
F.write (' xxx ') #只能写字符串

F.flush () #立即把缓冲区里面的内容写到磁盘里面

F.writelines () #写可迭代对象
F.tell () #获取当前文件指针的位置
F.close ()

python--file read/write

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.