Python Learning Notes (12) file read/write

Source: Internet
Author: User

Original chain: http://www.cnblogs.com/vamei/archive/2012/06/06/2537868.html

The first part:

#file read/write#f = open (file name, mode)#"R" read-only#"W" Write writesF= Open ("Test.txt","R")#Open Test.txt file, read-only mode#ReadContent =F.read (N)#read n bytes of data#the read ([size]) method reads a size byte from the current location of the file#If no parameter size is read to the end of the file, it is returned as a string objectcontent=F.readline ()#read a row#returns a String object#more suitable for large filesConten=F.readlines ()#Read all Rows#return list, each element is a row#large files Take time#WriteF.write ('I like Apple')#writing a string to a file#Close FileF.close" "the job builds a record.txt document that reads as follows: Tom, 86Lee, 99Lucy, 19, 56, read the file from the Record.txt and print it ." "" "additional Information linecache module output file line 2nd Text = linecache.getline (' A.txt ', 2) print (text)" "

Part II: Operations

File = Open ('D:/record.txt','W')Print(File.name) file.write ("' tom,12,86 ', ' lee,15,99 ', ' lucy,11,58 ', ' joseph,19,56 '") file.close () file= Open ('D:/record.txt','R') Lines=File.readlines ()Print(lines)#the run job is annotated#wrong line, newline character \ n#ModifyFile = Open ('D:/record.txt','W')Print(File.name) file.write ('tom,12,86\nlee,15,99\nlucy,11,58\njoseph,19,56') file.close () file= Open ('D:/record.txt','R') Lines=File.readlines ()Print(lines)#accomplished

Online editor run diagram, no directory, otherwise will be error

Modify the previous D drive File open diagram

Modified D-Drive File open diagram

Python Learning Notes (12) file read/write

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.