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 Paola/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, you use

text$ < fileName

where text$ is any string variable and fileName is an unquoted string. 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$ < height.inf
height = 'height$'

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

Writing a file

To write the contents of an existing string into a new text file, you use

text$ > fileName

where text$ is any string variable and fileName is an unquoted string. If the file cannot be created, the script terminates with an error message.

To append the contents of an existing string at the end of an existing text file, you use

text$ >> fileName

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

deleteFile (fileName$)

or with the directive

filedelete fileName

If the file does not exist, these commands do nothing.

The simplest way to append text to a file is by using fileappend:

fileappend out.txt Hello world!

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 collecting each line in a variable:

deleteFile ("squares.txt")
for i to 100
    square = i * i
    fileappend squares.txt The square of 'i' is 'square''newline$'
endfor

Note that we delete the file before appending to it, in order that we do not append to an already existing file.

If you put the name of the file into a variable, make sure to surround it with double quotes when using fileappend, since the file name may contain spaces and is not at the end of the line:

name$ = "C:/Documents and Settings/Paul Boersma/Desktop/squares.text"
filedelete 'name$'
for i to 100
    square = i * i
    fileappend "'name$'" The square of 'i' is 'square''newline$'
endfor

Finally, you can append the contents of the Info window to a file with

fappendinfo fileName

Directory listings

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

Links to this page


© ppgb, March 14, 2010