File creation

Hans Nowak wurmy at earthlink.net
Sat Nov 24 19:48:14 EST 2001


Aaron Sterling wrote:
> 
> Hi,
> 
>     To create files, I am currently using:
> 
>         temp_file = os.open(file_name, O_CREAT)
>         temp_file.close()
>         open(file_name, 'w')
> 
>     Is this the cannonical way of doing it?  If so, would it be usefull to have a function, create_file(file_name, mode, bufsize), linked to the interpreter, that
> would create the file and return an associated file object? Is their a better way to do it, or is it something that shouldn't be done, and if so why?

Why using os.open? It's best to use the built-in open function:

  f = open(filename, "wb")
  f.write("bla...\n")
  # etc
  f.close()

HTH,

--Hans



More information about the Python-list mailing list