我有一条相对的道路
$base_path = "input/myMock.TGZ";
Run Code Online (Sandbox Code Playgroud)
myMock.TGZ是位于输入文件夹中的文件名.文件名可以更改.但路径始终存储在$base_path.
我需要检查文件是否存在$base_path.
Gre*_*con 170
使用file-test运算符测试给定路径上是否存在某些内容-e.
print "$base_path exists!\n" if -e $base_path;
Run Code Online (Sandbox Code Playgroud)
但是,这个测试可能比您想要的更广泛.如果该路径中存在普通文件,则上面的代码将生成输出,但它也将触发目录,命名管道,符号链接或更奇特的可能性.有关详细信息,请参阅文档
鉴于.TGZ您的问题的扩展,似乎您期望一个普通文件而不是替代品.该-f文件的测试运营商要求的路径是否会导致一个纯文本文件.
print "$base_path is a plain file!\n" if -f $base_path;
Run Code Online (Sandbox Code Playgroud)
perlfunc文档涵盖了Perl文件测试操作符的长列表,涵盖了您在实践中将遇到的许多情况.
-r
文件可由有效的uid/gid读取.-w
文件可由有效的uid/gid写入.-x
文件可由有效的uid/gid执行.-o
文件由有效的uid拥有.-R
文件可由real uid/gid读取.-W
文件可由真实的uid/gid写入.-X
文件可由real uid/gid执行.-O
文件由real uid拥有.-e
文件已存在.-z
文件大小为零(为空).-s
文件具有非零大小(以字节为单位返回大小).-f
文件是普通文件.-d
文件是一个目录.-l
文件是符号链接(如果文件系统不支持符号链接,则为false).-p
File是命名管道(FIFO),或Filehandle是管道.-S
文件是一个套接字.-b
文件是块特殊文件.-c
文件是一个字符特殊文件.-t
Filehandle被打开了.-u
文件已设置setuid位.-g
文件已设置setgid位.-k
文件有粘滞位设置.-T
文件是ASCII或UTF-8文本文件(启发式猜测).-B
文件是"二进制"文件(与之相反-T).-M
脚本开始时间减去文件修改时间,以天为单位.-A
访问时间相同.-C
对于inode更改时间相同(Unix,可能因其他平台而异)
hpa*_*avc 29
你可能想要一个存在的变体... 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.
Run Code Online (Sandbox Code Playgroud)
mdm*_*mdm 16
if (-e $base_path)
{
# code
}
Run Code Online (Sandbox Code Playgroud)
-e 是Perl中的"存在"运算符.
您可以使用此页面上的代码检查权限和其他属性.
Zai*_*zvi 10
使用:
if (-f $filePath)
{
# code
}
Run Code Online (Sandbox Code Playgroud)
-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";
}
Run Code Online (Sandbox Code Playgroud)