[英]Open file in Tcl with variables inside
I want to open a file called filelist.txt
which contains only the string ${PATHFILE}/test.txt
, read the line and open the file test.txt
.我想打开一个名为
filelist.txt
的文件,其中只包含字符串${PATHFILE}/test.txt
,读取该行并打开文件test.txt
。 The file test.txt
is present inside the folder ~/testfile
.文件
test.txt
位于文件夹~/testfile
中。
Consider this sample code:考虑这个示例代码:
#!/usr/bin/env tclsh
set PATHFILE "~/testfile"
set fp [open "filelist.txt" r]
set lines [split [read $fp] "\n"]
close $fp
foreach line $lines {
set fp1 [open $line r]
close $fp1
}
The problem is that it seems that the "open" command cannot find the PATHFILE
variable and i get this error:问题是“打开”命令似乎找不到
PATHFILE
变量,我得到这个错误:
couldn't open "${PATHFILE}/test.txt": no such file or directory
If i try to open the file with set fp1 [open "${PATHFILE}/test.txt" r]
i don't have any errors.如果我尝试使用
set fp1 [open "${PATHFILE}/test.txt" r]
打开文件,我没有任何错误。
Yes, you can use the TCL subst command to evaluate the PATHFILE variable.是的,您可以使用TCL subst 命令评估 PATHFILE 变量。 Note that you might still have an issue with the tilde ~ - it may be better to use full path names.
请注意,波浪号 ~ 可能仍然存在问题 - 使用完整路径名可能更好。
#!/usr/bin/env tclsh
set PATHFILE "~/testfile"
set fp [open "filelist.txt" r]
set lines [split [read $fp] "\n"]
close $fp
foreach line $lines {
set fp1 [open [subst $line] r]
close $fp1
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.