无法打开目录,或者我的(Perl)代码有什么问题?

发布于 2024-08-23 20:17:09 字数 633 浏览 7 评论 0 原文

我有一个文件名 logspath.txt ,其中包含计算机上的日志目录路径。 每行一个目录。尽管该目录存在,但 Perl 说它不存在。

#!/usr/bin/perl -w

open FILE,"logspath.txt" or die $!;
while (<FILE>){
   print $_;
   opendir ($DIR,chomp($_)) or die $!;

当我运行脚本时,我得到:

/home/amosa/
No such file or directory at archive.pl line 6, <FILE> line 1.

列出目录:

~$ ls -l /home/amosa/
total 6
drwxr-xr-x  11 amosa    prodapp     1024 Mar  2 12:49 deploy
drwxr-xr-x   2 amosa    prodapp      512 Mar  2 12:39 lib
-rw-r--r--   1 amosa    prodapp      787 Mar  2 11:02 s

有什么建议吗?

I have a file name logspath.txt which contain on the logs directory path on the machine.
one directory each line. Although the directory exist, Perl say it doesn't.

#!/usr/bin/perl -w

open FILE,"logspath.txt" or die $!;
while (<FILE>){
   print $_;
   opendir ($DIR,chomp($_)) or die $!;

When I'm running the script I get:

/home/amosa/
No such file or directory at archive.pl line 6, <FILE> line 1.

Listing the directory :

~$ ls -l /home/amosa/
total 6
drwxr-xr-x  11 amosa    prodapp     1024 Mar  2 12:49 deploy
drwxr-xr-x   2 amosa    prodapp      512 Mar  2 12:39 lib
-rw-r--r--   1 amosa    prodapp      787 Mar  2 11:02 s

Any advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海的爱人是光 2024-08-30 20:17:09

chomp 没有有意义的返回值,您可以将其传递给 opendir。您需要在 opendir 上方的单独语句中 chomp 字符串。

 chomp;
 opendir DIR, $_ or die ...

chomp has no meaningful return value that you can then pass onto opendir. You need to chomp your string in a separate statement, above the opendir.

 chomp;
 opendir DIR, $_ or die ...
帝王念 2024-08-30 20:17:09

这应该是一条评论,但在评论中发布代码并没有真正起作用,所以我将其设为 CW。

以下是如何为更好的某些值编写此“更好”:

#!/usr/bin/perl

use strict; use warnings;

my $logs_file = 'logspath.txt';

open my $FILE, '<', $logs_file
    or die "Cannot open '$logs_file': $!";

while ( my $dir = <$FILE> ) {
    print $dir and chomp $dir;
    opendir my $dir_h, $dir
        or die "Cannot open directory '$dir': $!";
    # do something with $dir_h
}

简而言之,使用词法文件和目录句柄,使用 打开opendir 调用。

This should be a comment but posting code in comments does not really work so I am making it CW.

Here is how to write this "better" for some value of better:

#!/usr/bin/perl

use strict; use warnings;

my $logs_file = 'logspath.txt';

open my $FILE, '<', $logs_file
    or die "Cannot open '$logs_file': $!";

while ( my $dir = <$FILE> ) {
    print $dir and chomp $dir;
    opendir my $dir_h, $dir
        or die "Cannot open directory '$dir': $!";
    # do something with $dir_h
}

In short, use lexical file and directory handles, use the three argument form of open and include the name of the file or directory you were trying to open in the error message enclosed in quotation marks or brackets to see what was actually passed to the open or opendir call.

溺深海 2024-08-30 20:17:09

使用严格;
使用警告;

    open my $FILE, '<logspath.txt'
     or die "Cannot open '$logs_file': $!";

    while ( my $dir = <$FILE> ) {
        print $dir and chomp $dir;
            if( -d $dir)
            {
        opendir my $dir_h, $dir
            or die "Cannot open directory '$dir': $!";
        # do something with $dir_h
    }
     else
{
     print " Can't open directory $!\n";
}
}

这里“-d”用于检查目录是否可用。如果我们检查目录,它非常有用。

use strict;
use warnings;

    open my $FILE, '<logspath.txt'
     or die "Cannot open '$logs_file': $!";

    while ( my $dir = <$FILE> ) {
        print $dir and chomp $dir;
            if( -d $dir)
            {
        opendir my $dir_h, $dir
            or die "Cannot open directory '$dir': $!";
        # do something with $dir_h
    }
     else
{
     print " Can't open directory $!\n";
}
}

Here "-d" is used to check the directory is available or not. If we check the directory it is very useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文