Record some information to the file could be very usefull. For example, in order to debug or output statistics.

This is done in a few trivial steps:

1) open a file for writing

open() – This function returns a file object

For example,

f = open( ‘<filename>’, ‘<mode>’ ),

where filename – text file name

mode  – ‘w’ for writing, ‘r’ – for reading

2) to output data, use the command write

f.write( ‘This is a test\n’ )

in this case, the flag ‘\n’ means that after the outputed line,  a next line will be a new line. These flags are standard in general, you can watch them in any programming manual.

3) to complete the work with a file, it should be close


Python. Writing to text file
Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *