[Tutor] creating files

Alan Gauld alan.gauld at freenet.co.uk
Sun Nov 21 09:55:42 CET 2004


> encryption) and prints it to the screen.  The logic works fine, but
I can't
> save the output to a file.  ....  How do I make a
> variable that contains all of the letters combined in 1 string that
can be
> saved to a file?

The answer to your first question lies in the second.
To create a string use the append method (or string addition).
Once you have the string use the write() method to save it to a file.

Here is a simplified (untested!) example:

mystring = ''
# read 20 characters
for n in range(20):
    char = raw_input('Input a letter')[0]  # collect 1st letter
    mystring += char

# save mystring to myfile.txt
f = open('myfile.txt','w')
f.write(mystring)
f.close()

HTH.

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list