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

我有一个相对path

$base_path = "input/myMock.TGZ"; 

myMock.TGZ是位于input文件夹中的文件名。 文件名可以改变。 但是path总是存储在$base_path

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

使用-e文件testing运算符testing给定path中是​​否存在某些内容

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

但是,这个testing可能比您想要的要宽。 如果在该path上存在一个纯文本文件,上面的代码将生成输出,但是它也会激发一个目录,一个命名pipe道,一个符号链接或者一个更奇怪的可能性。 详细信息请参阅文档 。

鉴于在你的问题扩展.TGZ ,你似乎期望一个普通的文件,而不是替代品。 -f文件testing运算符询问path是否导致纯文件。

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

perlfunc文档涵盖了Perl文件testing操作符的一长串列表,涵盖了在实践中遇到的许多情况。

  • -r
    文件可以被有效的uid / gid读取。
  • -w
    文件可以通过有效的uid / gid写入。
  • -x
    文件可以由有效的uid / gid执行。
  • -o
    文件由有效的uid拥有。
  • -R
    文件可以被真正的uid / gid读取。
  • -W
    文件可以通过真正的uid / gid写入。
  • -X
    文件是由真正的uid / gid执行的。
  • -O
    文件是由真正的用户所有。
  • -e
    文件已存在。
  • -z
    文件大小为零(为空)。
  • -s
    文件具有非零大小(以字节为单位返回大小)。
  • -f
    文件是一个纯文件。
  • -d
    文件是一个目录。
  • -l
    文件是符号链接(如果文件系统不支持符号链接,则为false)。
  • -p
    文件是命名pipe道(FIFO),或者Filehandle是pipe道。
  • -S
    文件是一个套接字。
  • -b
    文件是块特殊文件。
  • -c
    文件是一个字符特殊文件。
  • -t
    文件句柄打开到tty。
  • -u
    文件设置了setuid位。
  • -g
    文件有setgid位设置。
  • -k
    文件有粘滞位设置。
  • -T
    文件是ASCII或UTF-8文本文件(启发式猜测)。
  • -B
    文件是“二进制”文件(与-T相反)。
  • -M
    脚本开始时间减去文件修改时间,以天为单位。
  • -A
    访问时间相同。
  • -C
    inode更改时间相同(Unix,其他平台可能不同)

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

  -X FILEHANDLE -X EXPR -X DIRHANDLE -XA 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 (-e $base_path) { # code } 

-e是Perl中的“存在”操作符。

您可以使用此页面上的代码检查权限和其他属性。

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

使用:

 if (-f $filePath) { # code } 

即使文件是目录, -e也会返回true。 -f只会返回true,如果它是一个实际的文件

 #!/usr/bin/perl -w $fileToLocate = '/whatever/path/for/file/you/are/searching/MyFile.txt'; if (-e $fileToLocate) { print "File is present"; } 
 if(-e $base_path){print "Something";} 

会做的伎俩

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

 print "File $base_path is exists!\n" if -f $base_path; 

享受