Write a function writeStringToFiles M filename that writes s

Write a function writeStringToFile(s, M, filename) that writes string s to a file called filename with M characters per line until reaching the end of s, and closes the file when finished. You can assume s does not contain any newline characters, \'\ \'.

Solution

def writeStringToFile(s,M,filename):
file = open(filename, \"wb\")
s=str(s)## converting into string
file.write(s.encode(\"utf-8\"))## need to use utf-8 other wise \'str\' does not support the buffer interface error get
## Closing file
file.close()
return s## returning string
## this is the test program
def test():
writeStringToFile(\"input into file\",10,\'filename.txt\')
test()

 Write a function writeStringToFile(s, M, filename) that writes string s to a file called filename with M characters per line until reaching the end of s, and c

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site