如果一个文件夹不存在,则强制生成一个文件夹

时间:2014-11-11 21:20:39

标签: perl file directory writing

如果在我的代码中第31行强制创建一个文件夹,当一个文件夹不存在时。我已经尝试过mkdir命令但无法使其工作。我也试过>>而不是一个。 这就是我所说的话:“打开我的$ fh,'>',”Ask / Parsed / Html / $“

 use lib '/Users/lialin/perl5/lib/perl5';
 use strict; use warnings;
 use feature 'say';
 use File::Slurp 'slurp';  # makes it easy to read files.
 use Mojo;
 use Mojo::UserAgent;
 use URI;

 #my $html_file = "Ask/Agilent_Technologies_ask.html";  # take file from directory
 #my $html_file = shift @ARGV; # take file from command lin

 my $calls_dir = "Ask/";
 opendir(my $search_dir, $calls_dir) or die "$!\n";
 my @html_files = grep /\.html$/i, readdir $search_dir;
 closedir $search_dir;
 #print "Got ", scalar @files, " files\n";

 #my %seen = ();
 foreach my $html_files (@html_files) {
 my %seen = ();
    my $current_file = $calls_dir . $html_files;
 open my $FILE, '<', $current_file or die "$html_files: $!\n";

 my $dom = Mojo::DOM->new(scalar slurp $calls_dir .$html_files);
 print $calls_dir .$html_files ;

 #for my $csshref ($dom->find('a[href]')->attr('href')->each) {
 #for my $link ($dom->find('a[href]')->attr('href')->each) {
 #  print $1;
 #say $1 #if $link->attr('href') =~ m{^https?://(.+?)/index\.php}s;
 open my $fh, '>', "Ask/Parsed/Html/${html_files}.result.txt" or die $!;
 for my $csshref ($dom->find('a[href]')->attr('href')->each) {
 my $cssurl = URI->new($csshref)->abs($calls_dir .$html_files);

 #open my $fh, '>', "Ask/${html_files}.result.txt" or die $!;
 $fh->print("$html_files\n");
 $fh->print("$cssurl\n");
 #$fh->print("\t"."$_\n");
 #print "$cssurl\n";
 #print $file."\t"."$_\n";

 }}

1 个答案:

答案 0 :(得分:1)

我建议使用File::Path,特别是make_path()。

你也可以用mkdir写一个递归函数,但在我看来它不值得麻烦,这个模块对我来说很好。