讀取文件
基本操作
set f [open e:/00 w] #用句柄f以寫的方式打開文件e;/00 若文件不存在打開新文件 puts $f "nihao" #將內容nihao輸出至句柄f close $f #關閉句柄f set f [open e:/00 r] #用句柄f以讀的方式打開文件e;/00 若文件不存在將創建 while {[gets $f line] >= 0} { #讀取一行內容 puts $f } #顯示該項內容 close $f #關閉句柄f set f [open e:/00 a] #用句柄f以追加的方式打開文件e;/00 若文件不存在將創建 puts $f "nihao" #將內容nihao輸出至句柄f close $f #關閉句柄f
http://blog.csdn.net/mvpme82/article/details/5405751 讀,寫,正則表達式
開頭判斷文件是否可以打開
set input_file "c://input.txt"
set output_file "c://output.txt"
if {[catch {set file_in [open $input_file r]} err_msg]} {
puts "Failed to open the file for reading: $err_msg"
return
}
if {[catch {set file_out [open $output_file w]} err_msg]} {
puts "Failed to open the file for writing: $err_msg"
close $file_in
return
}
逐行讀取文件並加入列表
set f [open temp.csv r]
set listVar {}
while {[gets $f line] != -1} {
puts $line
lappend listVar $line
}
puts $listVar
set listVar1 [lindex $listVar 1]
puts $listVar1