python-file Operations

Source: Internet
Author: User

File operations also read operations and write operations, we one by one.

Read File operation

In fact, and in the daily use of the file is the same, but to read files or write files, the first thing is to open the file .

FileRead = open ('/test.txt'R')   #r indicates that the format of the opening file is read mode

  open () is a python built-in function that throws an exception "IOerror"if the file does not exist.

In general, small files can be read directly using the Read () method, and the contents of the file are all read into memory at one time, and the type of str is returned.

However, sometimes the contents of the file may be very large, if the one-time read into the memory will be stuck, then you can use read (size) or readline (). SIZE Specifies the number of bytes per read. Get the full contents of a file through multiple loops.

 for inch #            print(line)

Finally, be sure not to forget to close the file. Remember to remember !

Fileread.close ()
Write file operations

Similar to read mode, write file Open file also need to be defined as W (write) mode , but at this time the open file is not really open, but to create a new file, if the previous file name, the original file will be overwritten by the existing file.

Loginfile = open ("login.txt""w")  Loginfile.write (  " write first line \ n") loginfile.write (" write second line \ n")  ) loginfile.close ()

At this point, open the Login.txt file, found that the previous content is not, the content is as follows:

Write the first line to write the second line

Then someone asked, how do I update the file. Of course, the pattern provided by Python also has a pattern called append (a) . The contents of write () in this mode will be updated after the original content.

Loginfile = open ("login.txt""a")  Loginfile.write (  " write line 3rd \ n") loginfile.close ()

When you open the login file, you will see:

Write the first line to write the second line to the 3rd line

However, if I want to modify the file , what should I do, there is no such model. Oh.. Of course--No. So how to modify it, it can only be modified to an intermediate file, and then write back. The code is as follows:

1Loginfile = open ("Login.txt","R") # Open the original file 2Loginfileupdate = open ("LoginUpdate.txt","W") #创建一个中间文件3  forLineinchLoginfile:4         ifUserNameinchLine : #找到修改位置进行修改5line ='I am NewUser'6 Loginfileupdate.write (line)7 loginfile.close ()8 loginfileupdate.close ()9  #write the contents of the intermediate file back to the original configuration fileTenLoginfile = open ("Login.txt","W") OneLoginfileupdate = open ("LoginUpdate.txt","R") A  forLineinchloginfileupdate: - Loginfile.write (line) - loginfile.close () theLoginfileupdate.close ()

Full list of open files in different modes:

Mode Description
R Open the file as read-only. The pointer to the file will be placed at the beginning of the file. This is the default mode.
Rb Opens a file in binary format for read-only. The file pointer will be placed at the beginning of the file. This is the default mode.
r+ Open a file for read-write. The file pointer will be placed at the beginning of the file.
rb+ Opens a file in binary format for read-write. The file pointer will be placed at the beginning of the file.
W Open a file for writing only. Overwrite the file if it already exists. If the file does not exist, create a new file.
Wb Open a file in binary format only for writing. Overwrite the file if it already exists. If the file does not exist, create a new file.
w+ Open a file for read-write. Overwrite the file if it already exists. If the file does not exist, create a new file.
wb+ Opens a file in binary format for read-write. Overwrite the file if it already exists. If the file does not exist, create a new file.
A Opens a file for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to.
Ab Opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to.
A + Open a file for read-write. If the file already exists, the file pointer will be placed at the end of the file. The file opens with an append mode. If the file does not exist, create a new file to read and write.
ab+ Opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, create a new file to read and write.

A list of all properties related to the file object:

Properties Description
File.closed Returns true if the file has been closed, otherwise false is returned.
File.mode Returns the access mode of the file being opened.
File.name Returns the name of the file.
File.softspace If the print output must be followed by a space character, false is returned. Otherwise, returns True.

  

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.