Scripting 6.4. Files

You can read from and write to text files from a Praat script.

Reading a file

You can check the availability of a file for reading with the function

fileReadable (fileName$)

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:/Users/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")

or

number = readFile ("myFile.txt")

If the file does not exist, the script terminates with an error message.

Example: reading a settings file

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$)
else
    height = 180
endif

Writing a file

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

createDirectory: directoryName$

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:/Users/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

deleteFile: fileName$

If the file does not exist, this command does nothing.

Example: writing a table of squares

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:

deleteFile: "squares.txt"
for i to 100
    appendFileLine: "squares.txt", "The square of ", i, " is ", i * i
endfor

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$ ( )

Directory listings

To get the names of the files if a certain type in a certain directory, use Create Strings as file list....

Alternative syntax

If, on the basis of the syntax of commands and functions in earlier sections you expected that

text$ = readFile$ ("myFile.txt")
number = readFile ("myFile.txt")

could be written as

text$ = readFile$: "myFile.txt"
number = readFile: "myFile.txt"

then you are right. The syntax with the colon is equivalent to the syntax with the two parentheses. Conversely, instead of

deleteFile: fileName$

you can also write

deleteFile (fileName$)

Links to this page


© ppgb, June 1, 2015