如何检查 Perl 中是否存在文件?

我有一条相对路径

   $base_path = "input/myMock.TGZ";

myMock.TGZ是位于 input 文件夹中的文件名。 文件名可以更改,但路径始终存储在 $base_path中。

我需要检查文件是否存在于 $base_path

300663 次浏览
if (-e $base_path)
{
# code
}

-e is the 'existence' operator in Perl.

You can check permissions and other attributes using the code on 这一页.

Test whether 什么的 exists at given path using the -e file-test operator.

print "$base_path exists!\n" if -e $base_path;

但是,这个测试可能比您想象的要广泛。如果该路径上存在一个普通文件,上面的代码将生成输出,但是它也会触发目录、命名管道、符号链接或更奇特的可能性。详情请参阅 See the documentation

考虑到 .TGZ在你的问题中的延伸,似乎你期望的是 普通文件而不是其他选择。-f文件测试操作符询问路径是否指向普通文件。

print "$base_path is a plain file!\n" if -f $base_path;

Perlfunc 文档涵盖了 Perl 的文件测试操作符的长列表,其中涵盖了您在实践中将遇到的许多情况。

  • -r
    文件可由有效的 uid/gid 读取。
  • -w
    文件可由有效的 uid/gid 写入。
  • -x
    文件可以通过有效的 uid/gid 执行。
  • -o
    文件由有效的 uid 所拥有。
  • -R
    File is readable by real uid/gid.
  • -W
    文件可由真正的 uid/gid 写入。
  • -X
    File is executable by real uid/gid.
  • -O
    文件由真正的 uid 所有。
  • -e
    文件存在。
  • -z
    文件大小为零(为空)。
  • -s
    文件大小不为零(以字节为单位返回大小)。
  • -f
    文件是普通文件。
  • -d
    文件是一个目录。
  • -l
    文件是一个符号链接(如果文件系统不支持符号链接,则为 false)。
  • -p
    File is a named pipe (FIFO), or Filehandle is a pipe.
  • -S
    File is a socket.
  • -b
    文件是块特殊文件。
  • -c
    File is a character special file.
  • -t
    文件句柄打开为 tty。
  • -u
    文件已设置 setuid 位。
  • -g
    文件设置了 setgid 位。
  • -k
    文件设置了粘贴位。
  • -T
    文件是 ASCII 或 UTF-8文本文件(启发式猜测)。
  • -B
    文件是一个“二进制”文件(与 -T相对)。
  • -M
    脚本开始时间减去文件修改时间(以天为单位)。
  • -A
    Same for access time.
  • -C
    Same for inode change time (Unix, may differ for other platforms)

你可以使用: if(-e $base_path)

你可能想要一个存在的变体... perldoc-f“-f”

      -X FILEHANDLE
-X EXPR
-X DIRHANDLE
-X      A file test, where X is one of the letters listed below.  This unary operator takes one argument,
either a filename, a filehandle, or a dirhandle, and tests the associated file to see if something is
true about it.  If the argument is omitted, tests $_, except for "-t", which tests STDIN.  Unless
otherwise documented, it returns 1 for true and '' for false, or the undefined value if the file
doesn’t exist.  Despite the funny names, precedence is the same as any other named unary operator.
The operator may be any of:


-r  File is readable by effective uid/gid.
-w  File is writable by effective uid/gid.
-x  File is executable by effective uid/gid.
-o  File is owned by effective uid.


-R  File is readable by real uid/gid.
-W  File is writable by real uid/gid.
-X  File is executable by real uid/gid.
-O  File is owned by real uid.


-e  File exists.
-z  File has zero size (is empty).
-s  File has nonzero size (returns size in bytes).


-f  File is a plain file.
-d  File is a directory.
-l  File is a symbolic link.
-p  File is a named pipe (FIFO), or Filehandle is a pipe.
-S  File is a socket.
-b  File is a block special file.
-c  File is a character special file.
-t  Filehandle is opened to a tty.


-u  File has setuid bit set.
-g  File has setgid bit set.
-k  File has sticky bit set.


-T  File is an ASCII text file (heuristic guess).
-B  File is a "binary" file (opposite of -T).


-M  Script start time minus file modification time, in days.

用途:

if (-f $filePath)
{
# code
}

-e returns true even if the file is a directory. -f will only return true if it's an actual file

#!/usr/bin/perl -w


$fileToLocate = '/whatever/path/for/file/you/are/searching/MyFile.txt';
if (-e $fileToLocate) {
print "File is present";
}

使用下面的代码。这里 -f检查它是否是一个文件:

print "File $base_path is exists!\n" if -f $base_path;
if(-e $base_path)
{
print "Something";
}

会有用的。