Debian Codegraph @

docs/manual/Scripting_6_4__Files.html

raw
1 <html><head><meta name="robots" content="index,follow"><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
2 <title>Scripting 6.4. Files</title>
3 <style>
4 td { padding-left: 5pt; padding-right: 5pt; }
5 th { padding-left: 5pt; padding-right: 5pt; }
6 code { white-space: pre-wrap; }
7 dd { white-space: pre-wrap; }
8 </style>
9 </head><body bgcolor="#FFFFFF">
10
11 <table border=0 cellpadding=0 cellspacing=0><tr><td bgcolor="#CCCC00"><table border=4 cellpadding=9><tr><td align=middle bgcolor="#000000"><font face="Palatino,Times" size=6 color="#999900"><b>
12 Scripting 6.4. Files
13 </b></font></table></table>
14 <p>You can read from and write to text files from a Praat script.</p>
15 <h2>Reading a file</h2>
16 <p>You can check the availability of a file for reading with the function</p>
17 <code> <b><a href="_fileReadable_.html">fileReadable</a></b> (<i>fileName$</i>)<br></code>
18 <p>which returns 1 (true) if the file exists and can be read, and 0 (false) otherwise. Note that <i><code><font size=+1>fileName$</font></code></i> is taken relatively to the folder where the script is saved; for instance, if your script is in the folder <code><font size=+1>Paolo/project1</font></code>, then the file name &#8220;hello.wav&#8221; refers to <code><font size=+1>Paolo/project1/hello.wav</font></code>, the file name &#8220;yesterday/hello.wav&#8221; refers to <code><font size=+1>Paolo/project1/yesterday/hello.wav</font></code>, and the file name &#8220;../project2/hello.wav&#8221; refers to <code><font size=+1>Paolo/project2/hello.wav</font></code> (&#8220;..&#8221; goes one folder up). You can also use full path names such as <code><font size=+1>C:/Users/Paolo/project1/hello.wav</font></code> on Windows and <code><font size=+1>/Users/Paolo/project1/hello.wav</font></code> on the Mac.</p>
19 <p>To read the contents of an existing text file into a string variable or into a numeric variable, you use</p>
20 <code> text$ = <b><a href="_readFile-S_.html">readFile$</a></b> (“myFile.txt”)<br></code>
21 <p>or</p>
22 <code> number = <b><a href="_readFile_.html">readFile</a></b> (“myFile.txt”)<br></code>
23 <p>If the file does not exist, the script terminates with an error message.</p>
24 <h2>Example: reading a settings file</h2>
25 <p>Suppose that the file <code><font size=+1>height.inf</font></code> may contain an appropriate value for a numeric variable called <code><font size=+1>height</font></code>, which we need to use in our script. We would like to read it with</p>
26 <code> height = <b><a href="_readFile_.html">readFile</a></b> (“height.inf”)<br></code>
27 <p>However, this script will fail if the file <code><font size=+1>height.inf</font></code> 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:</p>
28 <code> fileName$ = “height.inf”<br></code>
29 <code> <b>if</b> <b><a href="_fileReadable_.html">fileReadable</a></b> (fileName$)<br></code>
30 <code> height = <b><a href="_readFile_.html">readFile</a></b> (fileName$)<br></code>
31 <code> <b>else</b><br></code>
32 <code> height = 180<br></code>
33 <code> <b>endif</b><br></code>
34 <h2>Writing a file</h2>
35 <p>You write into a new text file just as you write into the Info window:</p>
36 <code> <b><a href="_writeFileLine_.html">writeFileLine</a></b>: “myFile.txt”, “The present year is ”, 2000 + 13, “.”<br></code>
37 <p>and likewise you use <a href="_writeFile_.html"><b><code><font size=+1>writeFile</font></code></b></a> 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.</p>
38 <p>To append text at the end of an existing file, you use</p>
39 <code> <b><a href="_appendFileLine_.html">appendFileLine</a></b>: “myFile.txt”, “Next year it will be ”, 2000 + 14, “.”<br></code>
40 <p>With <a href="_appendFileLine_.html"><b><code><font size=+1>appendFileLine</font></code></b></a> (and <a href="_appendFile_.html"><b><code><font size=+1>appendFile</font></code></b></a>, which does not add the newline), we follow the rule that if the file does not yet exist, it is created first.</p>
41 <p>You can create a folder (directory) with</p>
42 <code> <b><a href="_createFolder_.html">createFolder</a></b>: <i>folderPath$</i><br></code>
43 <p>where, as with file names, <i><code><font size=+1>folderPath$</font></code></i> can be relative to the folder of the script (e.g. &#8220;<code><font size=+1>data</font></code>&#8221;, or &#8220;<code><font size=+1>yesterday/data</font></code>&#8221;, or &#8220;<code><font size=+1>../project2/yesterday/data</font></code>&#8221;) or an absolute path (e.g. <code><font size=+1>C:/Users/Paolo/project1/yesterday/data</font></code> on Windows or &#8220;/Users/Paolo/project1/yesterday/data&#8221; on the Mac). If the folder already exists, this command does nothing.</p>
44 <p>You can delete an existing file with the function</p>
45 <code> <b><a href="_deleteFile_.html">deleteFile</a></b>: <i>fileName$</i><br></code>
46 <p>If the file does not exist, this command does nothing.</p>
47 <h2>Example: writing a table of squares</h2>
48 <p>Suppose that we want to create a file with the following 100 lines of text:</p>
49 <code> The square of 1 is 1<br></code>
50 <code> The square of 2 is 4<br></code>
51 <code> The square of 3 is 9<br></code>
52 <code> ...")<br></code>
53 <code> The square of 100 is 10000<br></code>
54 <p>We can do this by appending 100 lines:</p>
55 <code> <b><a href="_deleteFile_.html">deleteFile</a></b>: “squares.txt”<br></code>
56 <code> <b>for</b> i <b>to</b> 100<br></code>
57 <code> <b><a href="_appendFileLine_.html">appendFileLine</a></b>: “squares.txt”, “The square of ”, i, “ is ”, i * i<br></code>
58 <code> <b>endfor</b><br></code>
59 <p>Note that we delete the file before appending to it, in order that we do not append to an already existing file.</p>
60 <p>You can append the contents of the Info window to a file with</p>
61 <code> <b><a href="_appendFile_.html">appendFile</a></b>: “out.txt”, info$ ( )")<br></code>
62 <h2>Folder listings</h2>
63 <p>To get the names of the files if a certain type in a certain folder, use</p>
64 <code> fileNames$# = <b><a href="_fileNames-S-H_.html">fileNames$#</a></b>: <i>path$</i><br></code>
65 <p>For instance, to read in all the sound files in a specified folder, you could use the following script:</p>
66 <code> folder$ = “/usr/people/miep/sounds”<br></code>
67 <code> soundFileNames$# = <b><a href="_fileNames-S-H_.html">fileNames$#</a></b> (folder$ + “/*.wav”)<br></code>
68 <code> <b>for</b> ifile <b>to</b> <a href="_size_.html">size</a> (soundFileNames$#)<br></code>
69 <code> <a href="Read_from_file___.html">Read from file:</a> folder$ + “/” + soundFileNames$# [ifile]<br></code>
70 <code> <b>endfor</b><br></code>
71 <h2>Alternative syntax</h2>
72 <p>If, on the basis of the syntax of commands and functions in earlier sections you expected that</p>
73 <code> text$ = <b><a href="_readFile-S_.html">readFile$</a></b> (“myFile.txt”)<br></code>
74 <code> number = <b><a href="_readFile_.html">readFile</a></b> (“myFile.txt”)<br></code>
75 <p>could be written as</p>
76 <code> text$ = <b><a href="_readFile-S_.html">readFile$</a></b>: “myFile.txt”<br></code>
77 <code> number = <b><a href="_readFile_.html">readFile</a></b>: “myFile.txt”<br></code>
78 <p>then you are right. The syntax with the colon is equivalent to the syntax with the two parentheses. Conversely, instead of</p>
79 <code> <b><a href="_deleteFile_.html">deleteFile</a></b>: <i>fileName$</i><br></code>
80 <p>you can also write</p>
81 <code> <b><a href="_deleteFile_.html">deleteFile</a></b> (<i>fileName$</i>)<br></code>
82 <h3>Links to this page</h3>
83 <ul>
84 <li><a href="Scripting.html">Scripting</a>
85 <li><a href="Scripting_6__Communication_outside_the_script.html">Scripting 6. Communication outside the script</a>
86 </ul>
87 <hr>
88 <address>
89 <p>© Paul Boersma 2020-12-29</p>
90 </address>
91 </body>
92 </html>