Python Learning Files

Source: Internet
Author: User

1. file reading and writing

Python's function to read and write files is open or file

File_handler=open (Filename,,mode)

(1) Open and read files

Mode one: Open ()

Fo=open ('/root/test.txt') fo   # view fo info fo.read ()   # Read File contents fo.close ()   # close File

Mode two: File ()

Fo=file ('/root/test.txt') fo.read ()   # Read File contents
Fo.close () #关闭文件

(2) File write

The read-write mode when opening a file is shown in the following table:

Code One:

Fnew=open ('/root/new.txt','r+') fnew.read () Fnew.write ("new contents") fnew.close ()

Code two:

Fnew=open ('/root/new.txt','r+') fnew.write ("  New Contents") fnew.close ()

Code one in the end of the file appended to the content, code two is to overwrite the previous content, the difference is that the code more than one Fnew.read ()

2. File Object Methods

(1) Fileobject.close ()

(2) String=fileobject.readline ([size])

(3) List=fileobject.readlines ([size])

(4) String=fileobject.read ([size])

Size characters before reading a file

(5) Fileobject.next ()

Returns the current row and points the file pointer to the next line

(6) Fileobject.write (string)

(7) Fileobject.writelines (List)

Efficiency is higher than write, faster, and a few writes can use write

(8) Fileobject.seek (offset, option)

option = 0, which refers to pointing the file pointer from the file header to the "offset" byte

option = 1, which indicates that the file pointer points to the current position of the file and moves the "offset" byte backwards

option = 2, which means pointing the file pointer from the end of the file, moving the "offset" byte forward

(9) Fileobject.flush ()

3. File Operation Example

(1) Number of Hello in the statistics file

ImportREFP=file ("A.T","R") Count=0 forSinchfp.readlines (): Li=re.findall ("Hello", s)ifLen (LI) >0:count+=Len (LI)Print "Search"+count+"Hello"fp.close ()

(2) Replace the Hello in a.t with CSVT and save the result in a2.t

Fp1=file ("A.T","R") FP2=file ("a2.t","W") forSinchf1.readlines (): Fp2.write (S.replace ("Hello","CSVT")) Fp1.close () fp2.close ()

(3) Replace the Hello in a.t with the CSVT, and save the result to the original file

Fp1=file ("a.t","r+") for in Fp1.readlines ():        fp1.writeline (S.replace ("hello","  CSVT")) Fp1.close ()

4. Other applications for file operation

Directory Analyzer

Anti-virus software

System Garbage Cleanup Tool

Python Learning Files

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.