Python file Writelines () method

Source: Internet
Author: User
Overview

The Python file Writelines () method is used to write a sequence of strings to a file.

This sequence string can be generated by an iterative object, such as a list of strings.

Line breaks need to be made with a newline character \ n.

Grammar

The Writelines () method syntax is as follows:

Fileobject.writelines (iterable)
Parameters
  • iterable --Can iterate over objects (strings, lists, Ganso, dictionaries).

return value

The method has no return value.

Instance

The following example demonstrates the use of the Writelines () method:

#!/usr/bin/python3# Open the file fo = open ("Test.txt", "W", encoding= "Utf-8") print ("File name:", fo.name) seq = ["Rookie tutorial 1\n", "Rookie Tutorial 2"]fo . Writelines (SEQ) # Close File Fo.close ()

The result of the above example output is:

File name is:  test.txt

To view the contents of a file:

$ cat Test.txt Rookie Tutorial 1 rookie Tutorial 2

Python file Writelines () method

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.