Python file writing Basics

Source: Internet
Author: User

In front of the page, we learned how to read a file in Python and read and readline functions. In this section, we will learn how to write files in Python.


Here is an example to illustrate how to write a file. The function of this example is to write two strings to the B .txt file under the current directory.


 

 

 

 

The program code is as follows:


[Python]
Wfile = open ("B .txt", 'w ')
Wfile. write ("hello ")
Wfile. write ("www.jeapedu.com \ n ")
Wfile. close ()

Wfile = open ("B .txt", 'w ')
Wfile. write ("hello ")
Wfile. write ("www.jeapedu.com \ n ")
Wfile. close ()
The first line of the code is to open the B .txt file under the current directory in the format of writing, 2nd ~ Lines 3 are written to the "hello" and "www.jeapedu.com" strings respectively to the B .txt file. Line 2 of the code is to close the file. Careful readers will find that the parameters in line 2 of the Code are not the same as those in line 2 of the write code. Line 3 shows '\ n ', the Code in line 2 does not contain '\ n'. Here, the Code with' \ n' in line 3 is easy to read in readlinelines. The two strings are written in the same line in the B .txt file.

[Python]
Wfile = open ("B .txt", 'w ')
Wfile. write ("hello ")
Wfile. write ("www.jeapedu.com \ n ")
Wfile. close ()
Wfile = open ("B .txt", 'R ')
Word = wfile. readline ()
Print word
Wfile. close ()

Wfile = open ("B .txt", 'w ')
Wfile. write ("hello ")
Wfile. write ("www.jeapedu.com \ n ")
Wfile. close ()
Wfile = open ("B .txt", 'R ')
Word = wfile. readline ()
Print word
Wfile. close ()

Code 1st ~ Line 4: Open B .txt in the written format to write two strings, close the file, code 5th ~ Then open the B .txt file in the format of reading, read and output the file, and then close the B .txt file.
 

Now we have learned how to open a file and write a file. I want to raise a few questions .. If there is content in the file, can the old data be preserved every time new data is written? The above example shows that the data on the front side is not retained every time new data is written. This is terrible! What if I want to add new data to the old file?

Now let's look at the implementation code


[Python]
Wfile = open ("B .txt", 'A ')
Wfile. write ("hello ")
Wfile. write ("www.jeapedu.com \ n ")
Wfile. close ()
Wfile = open ("B .txt", 'R ')
Word = wfile. readline ()
Print word
Wfile. close ()

Wfile = open ("B .txt", 'A ')
Wfile. write ("hello ")
Wfile. write ("www.jeapedu.com \ n ")
Wfile. close ()
Wfile = open ("B .txt", 'R ')
Word = wfile. readline ()
Print word
Wfile. close ()

The only difference from the above program is that the first parameter (File opening mode word) of the 1st open function in the code line is changed to 'A ', the role of pattern word a here is that if there is data in the open file, the newly written data will be written at the end of the open file to add data.

 



Let's explain the figure above,

For the first time, cat B .txtis used to print a line of text in B .txt to the screen. For the first time, run the Python wfile.pyfile, write two strings, and output a line of B .txt content.


The second cat B .txtfile is used to print the B .txt file in the current directory to the screen. B .txt contains two lines of text (because two strings are written during the first run). The second run of Python wfile.pyfile, write two strings, and output one line of B .txt content.

The third cat B .txtfile is used to print the B .txt file in the current directory to the screen. There are three lines of text in B .txt (because two strings are written during the second run), and the third run Python wfile.pyfile, write two strings, and output one line of B .txt content.

It can be seen that the data written each time is added at the end of the file, the old data is retained, and new data is added.

 


 

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.