簡體   English   中英

如何在目錄名中使用通配符在Perl中打開文件?

[英]How can I open a file in Perl using a wildcard in the directory name?

我需要從一個目錄中打開一個名為OrthologousGroups.txt的文件,該目錄是根據創建它的日期命名的。 例如./Results_2016-2-7

有沒有辦法在路徑名中使用通配符來訪問我需要訪問的文件?

我嘗試了以下內容

open(FILE, "./Results*/OrthologousGroups.txt");

但是我收到了一個錯誤

關閉文件句柄上的readline()

使用glob獲取與glob模式匹配的文件名列表:

my @files = glob "./Results*/OrthologousGroups.txt";

if (@files != 1) {
    # decide what to do here if there are no matching files,
    # or multiple matching files
}

open my $fh, $files[0] or die "$! opening $files[0]";

# do stuff with $fh

作為一般說明,您應該檢查open是否成功 ,而不是簡單地假設它已經成功,那么當您實際上沒有首先打開文件時,您將不會獲得“readline on closed filehandle”錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM