Skip to content

CreateTemporaryFiles

dstoeckel edited this page Mar 16, 2015 · 2 revisions

How to create temporary files?

BALL offers a method !File::createTemporaryFilename().

C++

#include <BALL/SYSTEM/file.h>

...
  // allocate space for the temporary name
  String filename1 = "";
  String filename2 = ""; 

  // create a fileaname
  File::createTemporaryFilename(filename1);
  
  // create a file for writing 
  File file1(filename1, std::ios::out);
  ...
  file1.close();

  // create another file for writing 
  File::createTemporaryFilename(filename2);
  File file2(filename2, std::ios::out);
  ...
  file2.close();

  // finally remove the files
  File::remove(filename1);
  File::remove(filename2);

Note: All files have to be handled separately since otherwise the temporary filenames cannot be computed correctly.

Python

Python already offers a module tempfile to generate temporary files and directories. So lets use this :-)

import tempfile
import os
from BALL import *

# get a temporary file name
file,fname = tempfile.mkstemp()
os.close(file)

# use the name to create a BALL file. e.g. PDBFile
file = PDBFile(fname, File.MODE_OUT)
file.write(S)
file.close()

# don't forget to close and delete the file
file.close()
os.unlink(fname)
Clone this wiki locally