【问题标题】:Read all files in folder tcl/tk读取文件夹 tcl/tk 中的所有文件
【发布时间】:2021-12-30 17:04:06
【问题描述】:

我想读取所有包含 .sdc 的文件 该文件夹包括

alpha.sdc
beta.sdc
gamma.rpt

我试试 cmd

set a [open "proj/plrs/*.sdc" r]

但它不起作用

【问题讨论】:

标签: tcl tcltk


【解决方案1】:

@Andreas 有正确的想法。

set files [glob proj/plrs/*.sdc]
set combined ""
foreach file $files {
    set fh [open $file r]
    append combined [read $fh]
    close $fh
}

要将 glob 字符与 cat 一起使用,您需要一个 shell 来解释它们:

set combined [exec sh -c {cat proj/plrs/*.sdc}]

或展开glob的结果

set combined [exec cat {*}[glob proj/plrs/*.sdc]]

你可以使用tcllib

package require fileutil
set combined [fileutil::cat {*}[glob proj/plrs/*.sdc]]

请注意,glob 不会像 shell 那样对文件进行排序,所以您可能需要

set files [lsort [glob $pattern]]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-06
    • 1970-01-01
    • 2011-08-20
    • 1970-01-01
    • 2021-11-06
    • 2013-02-28
    • 1970-01-01
    相关资源
    最近更新 更多