Python file Operation two

Source: Internet
Author: User

ReadLines
Just like read without parameters, ReadLines can read the entire file in a row and return a list with the data of each row being an element

 #  coding=utf-8  f  = open ( "  Test.txt   ", "   R   " ) content  =  F.readlines ()  print   (type (content)) I  =1for  temp in   content:  print  (  " %d:%s   "% (i, temp)) I  +=1 f.close ()  

readline
reads one row of data at a time

 #  coding=utf-8  f  = open ( "  Test.txt   ", "   R   " ) content  =  F.readline ()  print  ( " 1:%s  " %content) content  = F.readline ()  Span style= "COLOR: #0000ff" >print  ( " 2:%s   "%content) f.close ()  

Tell
Get the current read and write location
In the process of reading and writing files, if you want to know the current location, you can use tell () to get

Seek
If you need to operate from another location while reading and writing files, you can use Seek ()
Seek (offset, from) has 2 parameters
Offset: Offsets
From: direction
0: Indicates the beginning of the file
1: Indicates the current position
2: Indicates end of file
When Offset>0, represents the right offset. When offset<0, indicates left offset

#open a file that already existsf = open ("Test.txt","R") Str= F.read (30)Print "The data read is:", str#Find Current LocationPosition =F.tell ()Print "Current file location:", Position#Reset LocationF.seek (5, 0)#Find Current LocationPosition =F.tell ()Print "Current file location:", Positionf.close ()

Python file Operation two

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.