Note
Python provides native support for manipulating files. Use that whenever possible to ensure your code is understandable by the greatest number of people.
fobj = h.File()
fobj = h.File("filename")
This class allows you to open several files at once, whereas the top level functions, ropen() and wopen() , allow you to deal only with one file at a time.
The functionality of xopen() is not implemented in this class so use
h.xopen(fobj.getname())
to execute a sequence of interpreter commands in a file.
Example:
from neuron import h f1 = h.File() //state that f1, f2, and f3 are pointers to the File class f2 = h.File() f3 = h.File() f1.ropen("file1") //open file1 for reading f2.wopen("file2") //open file2 for writing f3.aopen("file3") //open file3 for appending to the end of the fileopens file1, file2, and file3 for reading, writing, and appending (respectively).
Warning
The mswindows/dos version must deal with the difference between binary and text mode files. The difference is transparent to the user except for one limitation. If you mix text and binary data and you write text first to the file, then you need to do a File.seek(0) to explicitly switch to binary mode just after opening the file and prior to the first File.printf. An error message will occur if you read/write text to a file in text mode and then try to read/write a binary vector. This issue does not arise with the unix version.
See also
.ropen()
.ropen("name")
.wopen()
.wopen("name")
.aopen()
.aopen("name")
.xopen()
.xopen("name")
Open the file and execute it. (not implemented)
Note: if instead of a "name", the number 0,1,or 2 is specified then the stdin, stdout, or stderr is opened. (not implemented)
Example of creating a temporary file:
f = h.File() if f.mktemp() != 1: raise Exception('Unable to create temporary file') # create a tempoary file, get its name temp_file_name = f.getname() # do stuff, possibly using regular Python File IO instead # dispose of the temporary file f.unlink()
Reads the next number as in the function fscan() and returns its value.
Note: in order that .eof will return true after the last number, the last digit of that number should either be the last character in the file or be followed by a newline which is the last character in the file.
Read up to and including end of line. Returns length of string. If at the end of file, returns -1 and does not change the argument.
_ref_str is a reference to a NEURON string (e.g. one created via _ref_str = h.ref('')); it is not a Python string.
name = fobj.getname()
name = fobj.getname(strptr)
.chooser()
.chooser("w,r,a,x,d or nothing")
.chooser("w,r,a,x,d or nothing", "Banner", "filter", "accept", "cancel", "path")
File chooser interface for writing , reading, appending, or just specifying a directory or filename without opening. The banner is optional. The filter, eg. "*.dat" specifies the files shown in the browser part of the chooser. The "path" arg specifies the file or directory to use when the browser first pops up. The form with args sets the style of the chooser but does not pop it up. With no args, the browser pops up and can be called several times. Each time starting where it left off previously.
The "d" style is used for selecting a directory (in contrast to a file). With the "d" style, three buttons are placed beneath the browser area with Open centered beneath the Show, Cancel button pair. The Open button must be pressed for the dialog to return the name of the directory. The Show button merely selects the highlighted browser entry and shows the relevant directory contents. A returned directory string always has a final "/".
The "x" style is unimplemented. Use
f.chooser("", "Execute a hoc file", "*.hoc", "Execute")
if f.chooser():
h.xopen(f.getname())
Example:
from neuron import h, gui
f = h.File()
f.chooser('', 'Example file browser', '*', 'Type file name', 'Cancel')
while f.chooser():
print(f.getname())
The following comes courtesy of Zach Mainen, zach@helmholtz.sdsc.edu:
.vwrite(_ref_x)
.vwrite(n, _ref_x)
Write binary doubles to a file from an array or variable using fwrite(). The form with two arguments specifies the number of elements to write and the address from which to begin writing. With one argument, n is assumed to be 1. Must be careful that x[] has at least n elements after its passed address.
i.e. If x = h.Vector(10) and f is an instance of a File opened for writing, then one might call f.vwrite(5, x._ref_x[0] to write the first five values to a file.)
.vread(_ref_x)
.vread(n, _ref_x)
See also
.seek()
.seek(offset)
.seek(offset,origin)