Five steps to actual operation of Python read and write files

Source: Internet
Author: User

Python read and write files in the computer language is widely used, if you want to know the application of the program, the following article will give you a detailed introduction of the relevant content, you will be helpful in the process of learning later, we will detail its application.

First, open the file

Python read and write files in the computer language is widely used, if you want to know the application of the program, the following article will give you a detailed introduction of the relevant content, you will be helpful in the process of learning later, we will detail its application.
The code is as follows:

    1. f = open ("D:\test.txt", "W")

Description

The first parameter is the file name, including the path, and the second argument is the open mode
' R ': Read-only (default. Throws an error if the file does not exist)
' W ': Write-only (if the file does not exist, the file is created automatically)
' A ': Append to end of file
' r+ ': Read and Write
If you need to open the file in binary mode, you need to add the character "B" after mode, such as "RB", "WB", etc.
Second, read the content

    1. F.read (size)

The parameter size indicates the number of reads, which can be omitted. If the size parameter is omitted, all contents of the file are read.

    1. F.readline ()

Reads the contents of a file line

    1. F.readlines ()

Read all rows into the array inside [line1,line2,... linen]. This approach is often used to improve efficiency by avoiding the loading of all file content into memory.

Third, write the file

    1. F.write (String)

Writes a string to the file, and if the write ends, you must add "\ n" after the string, and then F.close () to close the file


Iv. content positioning in the document

    1. F.read ()

After reading, the file pointer reaches the end of the file, and if you do it again f.read () will find empty content, and if you want to read everything again, you must move the anchor pointer to the beginning of the file:

    1. F.seek (0)

The format of this function is as follows (in bytes):

    1. F.seek (offset, from_what)

From_what represents the start of the read position, offset means moving from from_what to a certain distance, such as F.seek (10, 3) is positioned to the third character and then 10 characters later. A from_what value of 0 indicates the beginning of the file, which can also be omitted, and by default 0 is the beginning of the file. The following gives a

      1. f  = open ('/tmp/workfile ',  ' r+ ')  
      2. f.write (' 0123456789abcdef ')  
      3. f.seek (5)  #  go to the 6th byte in the file  
      4. f.read (1)    
      5. ' 5 '  
      6. Span style= "FONT-SIZE:16PX;" >f.seek  ( -3, 2)  # go to the 3rd byte before the end  
      7. f.read (1)  
      8. ' d '  
      9.  

Python read and write files in the five steps of actual operation

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.