YouTip LogoYouTip

File Writelines

# Python2.x Python File writelines() Method [![Image 3: Python File Methods](#) Python File Methods](#) * * * ### Overview The **writelines()** method is used to write a sequence of strings to a file. This sequence of strings can be generated by an iterable object, such as a list of strings. To insert newlines, you must specify the newline character n. ### Syntax The syntax for the writelines() method is as follows: fileObject.writelines( ) ### Parameters * **str** -- A sequence of strings to be written to the file. ### Return Value This method does not return any value. ### Example The following example demonstrates the use of the writelines() method: #!/usr/bin/python# -*- coding: UTF-8 -*-# Open the file fo = open("test.txt", "w")print "File name: ", fo.name seq = [" 1n", " 2"] fo.writelines( seq )# Close the file fo.close() The output of the above example is: File name: test.txt To view the file content: $ cat test.txt 1 2 [![Image 4: Python File Methods](#) Python File Methods](#)
← Os AccessFile Writelines β†’