[Tutor] creating text files

Michael Janssen Janssen at rz.uni-frankfurt.de
Thu Dec 11 07:45:43 EST 2003


On Thu, 11 Dec 2003, ali wrote:

> am a newbie and was wondering how to create a new text file in python...

Hello Ali,


you need to open a new file and then write to it:

file_object = open("file-name", "w") # "w" means open in write mode

file_object.write("Write some text\n") # "\n" is a newline

file_object.close() # after work is done close the file


Best is to compare the Tutorial:
http://www.python.org/doc/current/tut/tut.html

for details. Section 7.2 . Make some tests with the mode argument (like
"w", "a", "r") and with read, readlines and writelines methods of file
objects. Under Windows you might want to use modes like "wb" (check the
tutorial for this).


Michael




More information about the Tutor mailing list