You can read from and write to text files from a Praat script.
You can check the availability of a file for reading with the function
which returns 1 (true) if the file exists and can be read, and 0 (false) otherwise. Note that fileName$ is taken relatively to the directory where the script is saved; for instance, if your script is in the directory Paolo/project1, then the file name "hello.wav" refers to Paolo/project1/hello.wav, the file name "yesterday/hello.wav" refers to Paolo/project1/yesterday/hello.wav, and the file name "../project2/hello.wav" refers to Paolo/project2/hello.wav (".." goes one directory up). You can also use full path names such as "C:/Documents and Settings/Paolo/project1/hello.wav" on Windows and "/Users/Paolo/project1/hello.wav" on the Mac.
To read the contents of an existing text file into a string variable or into a numeric variable, you use
text$ = readFile$ ("myFile.txt")
number = readFile ("myFile.txt")
If the file does not exist, the script terminates with an error message.
Suppose that the file height.inf may contain an appropriate value for a numeric variable called
height, which we need to use in our script. We would like to read it with
height = readFile ("height.inf")
However, this script will fail if the file height.inf does not exist. To guard against this situation, we could check the existence of the file, and supply a default value in case the file does not exist:
fileName$ = "height.inf"
if fileReadable (fileName$)
height = readFile (fileName$)
height = 180
You write into a new text file just as you write into the Info window:
writeFileLine ("myFile.txt", "The present year is ", 2000 + 13, ".")
and likewise you use writeFile if you don't want a newline symbol at the end of the file. If the file cannot be created, the script terminates with an error message.
To append text at the end of an existing file, you use
appendFileLine ("myFile.txt", "Next year it will be ", 2000 + 14, ".")
With appendFileLine (and appendFile, which does not add the newline), we follow the rule that if the file does not yet exist, it is created first.
You can create a directory with
where, as with file names, directoryName$ can be relative to the directory of the script (e.g. "data", or "yesterday/data", or "../project2/yesterday/data") or an absolute path (e.g. "C:/Documents and Settings/Paolo/project1/yesterday/data" on Windows or "/Users/Paolo/project1/yesterday/data" on the Mac). If the directory already exists, this command does nothing.
You can delete an existing file with the function
If the file does not exist, this command does nothing.
Suppose that we want to create a file with the following text:
The square of 1 is 1
The square of 2 is 4
The square of 3 is 9
The square of 100 is 10000
We can do this by appending 100 lines:
for i to 100
appendFileLine ("squares.txt", "The square of ", i, " is ", i * i)
Note that we delete the file before appending to it, in order that we do not append to an already existing file.
You can append the contents of the Info window to a file with
appendFile ("out.txt", info$ ())
To get the names of the files if a certain type in a certain directory, use Create Strings as file list....
© ppgb, May 1, 2013