📄 scripting_6_5__files.html
字号:
<html><head><meta name="robots" content="index,follow">
<title>脚本 6.5. 文件</title></head><body bgcolor="#FFFFFF">
<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>
脚本 6.5. 文件
</b></font></table></table>
<p>
可以用Praat脚本读取和写入文件。</p>
<h3>
读取文件</h3>
<p>
要检查一个文件能否读取,会用到函数</p>
<code>
<b>fileReadable</b> (fileName$)<br></code>
<p>
文件存在且可读,返回<b>true</b>;否则返回<b>false</b>。</p>
<p>
从一个存在的文件中读取内容并存入字符串变量,可以这样:</p>
<code>
text$ <b><</b> <i>文件名</i><br></code>
<p>
这里的<code>text$</code>是一个任意的字符串,而<code><i>文件名</i></code>是一个不加括引的字符串。如果该文件不存在,脚本将终止并提示出错消息。</p>
<h3>
示例:读取一个设置文件</h3>
<p>
假定文件<b>height.inf</b>包含适合数值变量<code>height</code>的值,需要在脚本中用到。我们可以这样读取:</p>
<code>
height$ < height.inf<br></code>
<code>
height = 'height$'<br></code>
<p>
但如果<b>height.inf</b>文件不存在,脚本就会崩溃。为了避免出现这种状况,应当检查文件是否存在,并为文件可能不存在预备一个缺省值:</p>
<code>
fileName$ = "height.inf"<br></code>
<code>
if fileReadable (fileName$)<br></code>
<code>
height$ < 'fileName$'<br></code>
<code>
height = 'height$'<br></code>
<code>
else<br></code>
<code>
height = 180<br></code>
<code>
endif<br></code>
<h3>
写入文件</h3>
<p>
将一个已有字符串的内容写入一个新建的文件,可以这样:</p>
<code>
text$ <b>></b> <i>文件名</i><br></code>
<p>
这里的<code>text$</code>是一个任意的字符串,而<code><i>文件名</i></code>是一个不加括引的字符串。如果该文件无法创建,脚本将终止并提示出错消息。</p>
<p>
想要向现存的文本文件末尾追加一个已有的字符串的内容,可以这样:</p>
<code>
text$ <b>>></b> <i>文件名</i><br></code>
<p>
如果该文件尚不存在,将会先行创建。</p>
<p>
可以删除一个现在的文件,只要用:</p>
<code>
<b>filedelete</b> <i>文件名</i><br></code>
<p>
如果该文件不存在,<b>filedelete</b>命令什么也不会做。</p>
<p>
向文件追加文本,最简单的方法是使用<b>fileappend</b>命令:</p>
<code>
<b>fileappend</b> out.txt Hello world!<br></code>
<h3>
示例:写平方表</h3>
<p>
假如要创建含有以下文本的文件:</p>
<code>
The square of 1 is 1<br></code>
<code>
The square of 2 is 4<br></code>
<code>
The square of 3 is 9<br></code>
<code>
...<br></code>
<code>
The square of 100 is 10000<br></code>
<p>
只要用一个变量来收集每一行:
We can do this by collecting each line in a variable:</p>
<code>
filedelete squares.txt<br></code>
<code>
for i to 100<br></code>
<code>
square = i * i<br></code>
<code>
fileappend squares.txt The square of 'i' is 'square''newline$'<br></code>
<code>
endfor<br></code>
<p>
注意:我们在追加内容之前先删掉了文件,这是为了避免向一个已经存在的文件追加内容。</p>
<p>
使用<b>fileappend</b>命令时,如果文件名存放在变量里,应保证用双引号括引起来,因为文件名很可能包含空格,而且并不在行末:</p>
<code>
name$ = "Hard disk:Desktop Folder:squares.text"<br></code>
<code>
filedelete 'name$'<br></code>
<code>
for i to 100<br></code>
<code>
square = i * i<br></code>
<code>
fileappend "'name$'" The square of 'i' is 'square''newline$'<br></code>
<code>
endfor<br></code>
<p>
最后,还可以将Info窗口的全部内容追加进一个文件,只要用:</p>
<code>
<b>fappendinfo</b> <i>文件名</i><br></code>
<h3>
目录列表</h3>
<p>
要获取指定目录内指定类型的文件名称清单,请使用<a href="Create_Strings_as_file_list___.html">Create Strings as file list...(创建文件清单字符串集)</a>命令。</p>
<h3>指向本页的链接</h3>
<ul>
<li><a href="Scripting.html">脚本</a>
<li><a href="Scripting_6__Communication_outside_the_script.html">脚本 6. 脚本外通信</a>
</ul>
<hr>
<address>
<p>© ppgb, August 21, 2001</p>
<p>© 翻译:徐清白,2005年06月06日</p>
</address>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -