如何在Perl中创建和附加具有可变路径的文件?

时间:2009-07-31 18:28:35

标签: perl file path

我正在努力创建一个脚本,为我创建图像库。

当我运行我所拥有的东西时它会告诉我

No such file or directory at photographycreate line 16.
------------
(program exited with code: 2)

这是我到目前为止所获得的代码。

#!/etc/perl -w

#CHANGE THIS
$filecategory = "cooking";

$filenumber = 0;
#$filename = "photography";
$imagedirectory = "\"/media/New Volume/Programming/kai product/media/photography/".$filecategory."/images/\"";
$galleryfile = "\"/media/New Volume/Programming/kai product/pages/".$filenumber."_".$filecategory."_gallery.html\"";

@imagelocation = <$imagedirectory/*>; #*/
$filecount = @imagelocation;

while($filenumber < 3) {        
    open GALLERY, "+>", $galleryfile or die $!;
    print GALLERY ($filecount."\n");
    print GALLERY ($imagedirectory."\n");
    print GALLERY ($galleryfile."\n");
    close GALLERY;

    ++$filenumber;
}

我想要它做的是创建文件,打开它,写东西,然后关闭/保存它。我怎么能用我所拥有的呢?

以下是修复:

#!/etc/perl -w
use Fcntl; #The Module
use strict;

#CHANGE THIS
my $filecategory = "cooking";

my $filenumber = 0;
my $imagedirectory = "\"/media/New Volume/Programming/kaiproduct/media/photography/".$filecategory."/images/\"";
my $galleryfile = "/media/New Volume/Programming/kaiproduct/pages/".$filenumber."_".$filecategory."_gallery.html";

my @imagelocation = <$imagedirectory/*>; #*/
my $filecount = @imagelocation;

while($filenumber < 3)
    {
        open GALLERY, "+>", $galleryfile or die $!;
        print (GALLERY $filecount."\n");
        print (GALLERY $imagedirectory."\n");
        print (GALLERY $galleryfile."\n");
        close GALLERY;

        ++$filenumber;
    }

2 个答案:

答案 0 :(得分:7)

我认为问题在于:

$imagedirectory = "\"/media/New Volume/Programming/kai product/media/photography/".$filecategory."/images/\"";
$galleryfile    = "\"/media/New Volume/Programming/kai product/pages/".$filenumber."_".$filecategory."_gallery.html\"";

具体来说,这些字符串中的每一个都以"\"开头,以\""结尾,这意味着您的文件和文件夹将被双引号括起来。因此,Perl不会尝试打开/media/New Volume/etc...,而是"/media/New Volume/etc...",因为没有名为"的目录,所以它不存在。你过度引用了。

您可以(而且应该始终)做的一件事就是use strict;。我看到你的顶部已经有use warnings;,这很好,但同时使用strictwarnings会让您的代码更安全,更好看。

答案 1 :(得分:2)

IMNHO,真正的答案是使用File::Spec并写得更清楚:

use File::Spec::Functions qw( catfile );

# ...

my $root = "/media/New Volume/Programming/kai product";

my $imagedirectory = catfile($root,
    'photography',
    $filecategory,
    'images',
);

my $galleryfile = catfile($root,
    'pages',
    "${filenumber}_${filecategory}_gallery.html",
);

此外,观察良好习惯是个好主意,特别是因为你刚刚学习Perl:

始终放置:

use strict;
use warnings;

是您计划中的第一件事。

使用词汇文件句柄 而不是裸字文件句柄(包全局):

open my $gallery, '+>', $galleryfile 
    or die "Cannot open '$galleryfile': $!;

并在错误消息中包含该文件的名称。

最后,我喜欢File::Slurpappend_file

append_file $gallery_file, [ 
    map { "$_\n" } ( $filecount, $imagedirectory, $galleryfile )
];

以下是您的计划的修订版本:

#!/etc/perl

use strict;
use warnings;

use File::Spec::Functions qw( catfile );

my $root = "/media/New Volume/Programming/kai product";
my $filecategory = "cooking";

my $imagedirectory = catfile($root,
    'photography',
    $filecategory,
    'images',
);

my @imagelocation = read_dir $imagedirectory;

for my $filenumber ( 0 .. 2 ) { 

    my $galleryfile = catfile($root, 'pages',
        "${filenumber}_${filecategory}_gallery.html",
    );

    append_file $gallery_file, [ 
        map { "$_\n" } ( 
            scalar @imagelocation, $imagedirectory, $galleryfile,
        )];
}