0

File problem 1.
Write a function named stringsToFile() that opens a file for writing and writes a list of strings to the file, with each element of the list written to a line. Hint: use a for loop to iterate over the list of strings and write them to the file.
Input (two parameters):
* filename -- name of file to open for writing * strlist -- a list of string to write to filename
An example of how to call stringToFile() is:
s0 = 'You are old Father William, the young man said,' s1 = 'And your hair has become very white;'
s2 = 'And yet you incessantly stand on your head--'
s3 = 'Do you think, at your age, it is right?'
lst = [s0, s1, s2, s3]
stringsToFile('FatherWilliam.txt', lst)

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by pyTony
0

Hint stringsToFile is not Python standard name (strings_to_file is), instead for loop you can use built in method of file type adn string type. Do not forget that recommended way to deal with files is with with

Remember to uses meaningful variable names and do not use variables just to obfuscate the code like s-variables in your post.

Edited by pyTony

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.