Answers:
使用文件测试运算符测试在给定路径中是否存在某些内容-e
。
print "$base_path exists!\n" if -e $base_path;
但是,此测试可能比您预期的要广泛。如果在该路径上存在一个普通文件,则上面的代码将生成输出,但也会为目录,命名管道,符号链接或更特殊的可能性触发。请参阅说明文件详细信息,。
考虑到.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
真实的uid / gid可以读取文件。-W
文件可由真实的uid / gid写入。-X
文件可以由真实的uid / gid执行。-O
文件由真正的uid拥有。-e
文件已存在。-z
文件大小为零(为空)。-s
文件的大小为非零(返回大小以字节为单位)。-f
文件是纯文件。-d
文件是目录。-l
文件是符号链接(如果文件系统不支持符号链接,则为false)。-p
File是命名管道(FIFO),或者Filehandle是管道。-S
文件是套接字。-b
文件是块特殊文件。-c
文件是字符特殊文件。-t
Filehandle向tty打开。-u
文件已设置setuid位。-g
文件已设置setgid位。-k
文件设置了粘性位。-T
文件是ASCII或UTF-8文本文件(启发式猜测)。-B
文件是“二进制”文件(与相对-T
)。-M
脚本开始时间减去文件修改时间(以天为单位)。-A
访问时间相同。-C
inode更改时间相同(Unix,其他平台可能有所不同)
-e
可以使用相对路径,但是我认为我可能误解了您的问题。您是否有一个名为的目录myMock.TGZ
,并且想知道该目录是否包含具有特定名称的文件?通过编辑问题以包括示例,帮助我们为您提供更好的答案!
您可能希望存在一个变体... 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
即使文件是目录也返回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;
享受