Chapter 14 – Python File Write Operations

Step 1
Open any text editor (gedit,pico,vi,nano) then enter the following python script and save the file with .py extension.
#Author : Jijo K Jose
print "\n————————————\nChapter 14 – File Writing \n————————————\n"
h1 = open("data1","r")
h2 = open("data2","w")
h3 = open("data3","w")
print h1
h1 = open("data1","r")
s = h1.readlines()
print "\nReading the Content – readlines() : ",s
for i in s:
print "File contents are written to data2 using write()"
print "File contents are written to data3 using writelines()"
print "\n————————————–\n"

Step 2
Change file permission to executable using chmod command.
Step 3
Execute the python file.

You can leave a response, or trackback from your own site.

Leave a Reply