How to creat a file?

Juho Schultz juho.schultz at helsinki.fi
Fri Dec 2 08:07:09 EST 2005


sandorf wrote:
> I'm new to python. Have a simple question.
> 
> "open" function can only open an existing file and raise a IOerror when
> the given file does not exist. How can I creat a new file then?
> 

You already have two correct answers. A warning: if you open a existing
file for writing, it is truncated (the old contents of that file
disappear.) For adding new content to the end of an existing file,
you can use:

f = open(filename,'a')



More information about the Python-list mailing list