newfile – New measurement folder creation¶
Method for opening a new file for writing
This module contains a single method used for creating a new datafile. This method handles timestamping the measurement folder, keeping track of the index of the measurement (if desired) and makes backup copies of the previously run scripts for reference.
-
stlabutils.newfile.
newfile
(prefix, idstring, colnames=None, mypath='./', usedate=True, usefolder=True, autoindex=False, return_folder_name=False, git_id=True)[source]¶ Creates a new file for storing data.
By default will create a folder (at the location of the running script) with a new file open for writing and a copy of the script the function was called from. The naming scheme for the folder is
<prefix>_yy_mm_dd_HH.MM.SS_<idstring>
. The file is named the same with a “.dat” extension.Generally, if using default options, overwriting an existing file should not happen (date and time will never be exactly equal). However, if the desired filename already exists, this method will overwrite the old file.
- Parameters
prefix (str) – A string to be placed in front of the timestamp in the filename. Can be blank or None but must be specified
idstring (str) – A string to be placed behind the timestamp. Can also be blank or None but must be specified
colnames (list of str, optional) – Array-like containing column titles for data. This will be written in the first line of the file delimited by ‘, ‘. As an example, if colnames = [‘abc’, ‘def’, ‘ghi’], the first line in the file will be
"# abc, def, ghi\n"
. By default is left blankmypath (str, optional) – Path for folder (or file) creation if pwd is not the desired path. By default it is pwd.
usedate (bool, optional) – Boolean to include timestamp. If False, the timestamp is excluded from the file/folder name. True by default
usefolder (bool, optional) – If set to False the file will be opened at the specified location with the usual naming but with no subfolder and no copy will be made of the running script. True by default
autoindex (bool, optional) – Specifies if indexing of successively opened files is desired. Will add a running index to the prefix of the newly created file/folder. This will be incremented by 1 for each new file with the same prefix. If no files are found with the same prefix, it creates the first file name <prefix>1_yy_mm_dd_HH.MM.SS_<idstring>. Successive files will be named <prefix><idx>_yy_mm_dd_HH.MM.SS_<idstring>. Is False by default.
git_id (bool, optional) – Boolean to query and save the git id of stlab
- Returns
myfile – Open file handle for writing
- Return type
_io.TextIOWrapper