问题
I need to open a file called OrthologousGroups.txt
from a directory that is named based on the date of when it is created. e.g. ./Results_2016-2-7
Is there a way to use a wildcard in the path name to the file I need to access?
I tried the following
open(FILE, "./Results*/OrthologousGroups.txt");
but I get an error
readline() on closed filehandle
回答1:
Use glob
to get a list of filenames matching the glob pattern:
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
As a general note, you should check whether open succeeded, instead of simply assuming it did, then you won't get "readline on closed filehandle" errors when you didn't actually open a file in the first place.
来源:https://stackoverflow.com/questions/35471597/how-can-i-open-a-file-in-perl-using-a-wildcard-in-the-directory-name