Python file operations

Source: Internet
Author: User

1. Open the Mode list

#Open the Mode list:#W opens in write mode,#a opens in Append mode (starting with EOF and creating a new file if necessary)#r+ Open in read-write mode#w+ Open in read/write mode (see W)#A + opens in read/write mode (see a)#RB opens in binary read mode#WB opens in binary write mode (see W)#AB opens in binary append mode (see a)#rb+ opens in binary read/write mode (see r+)#wb+ opens in binary read/write mode (see w+)#ab+ opens in binary read/write mode (see A +)f = open ('/users/kamil/pycharmprojects/s20160115_1/day1/if_el/test.log','w+') forIinchRange (10): F.write ('liukun\n') F.close ()

2. File Read
f = open (' Tpm.txt ') # Read mode reads S = F.read () print (S, ' \n\n\n ') print (F.tell ()) #上面读取完后指针移动到最后, move the file pointer through seek to the file header F.seek (0) # Use ReadLine to read one row at a time (True): Line    = F.readline ()    print (lines)    if (len) = = 0): Break        F.close ()

Python file operations

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.