Answer to Question #264212 in Python for Tori

Question #264212

Write a program to write info from a txt. file. You are to create a file that writes the text from your file and puts "*" on either side of your text. The number of stars on each side is to equal the line number from the text you read. ie. line 10 has 10 stars on either side - **********Introducing*********. Your file is to end when the program reads EXIT.


1
Expert's answer
2021-11-12T16:32:01-0500
# Program to show various ways to read and 
# write data in a file. 

file1 = open("myfile.txt","w") 


L = ["This is Delhi \n","This is Paris \n","This is London \n"]  


  
# \n is placed to indicate EOL (End of Line) 

file1.write("Hello \n") 

file1.writelines(L) 

file1.close() #to change file access modes 


  

file1 = open("myfile.txt","r+")  


  

print "Output of Read function is "


print file1.read() 

print

  
# seek(n) takes the file handle to the nth 
# bite from the beginning. 

file1.seek(0)  


  

print "Output of Readline function is "


print file1.readline() 

print

  

file1.seek(0) 


  
# To show difference between read and readline 

print "Output of Read(9) function is "


print file1.read(9) 

print

  

file1.seek(0) 


  

print "Output of Readline(9) function is "


print file1.readline(9) 


  

file1.seek(0) 

# readlines function 

print "Output of Readlines function is "


print file1.readlines() 

print
file1.close()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS