【发布时间】:2011-02-05 18:14:06
【问题描述】:
我有一个相对路径
$base_path = "input/myMock.TGZ";
myMock.TGZ 是位于输入文件夹中的文件名。
文件名可以更改。但是路径总是存储在$base_path中。
我需要检查$base_path中是否存在该文件。
【问题讨论】:
标签: perl
我有一个相对路径
$base_path = "input/myMock.TGZ";
myMock.TGZ 是位于输入文件夹中的文件名。
文件名可以更改。但是路径总是存储在$base_path中。
我需要检查$base_path中是否存在该文件。
【问题讨论】:
标签: perl
使用-e file-test 运算符测试 something 是否存在于给定路径。
print "$base_path exists!\n" if -e $base_path;
但是,此测试可能比您预期的要广泛。如果该路径中存在一个普通文件,上面的代码将生成输出,但它也会为目录、命名管道、符号链接或更奇特的可能性触发。 See the documentation了解详情。
鉴于您的问题中.TGZ 的扩展名,您似乎期望普通文件 而不是替代品。 -f file-test 运算符询问路径是否指向普通文件。
print "$base_path is a plain file!\n" if -f $base_path;
perlfunc 文档涵盖了Perl's file-test operators 的长列表,涵盖了您在实践中会遇到的许多情况。
-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
File 是一个符号链接(如果文件系统不支持符号链接,则为 false)。-p
File 是命名管道 (FIFO),或者 Filehandle 是管道。-S
文件是一个套接字。-b
文件是块特殊文件。-c
文件是字符特殊文件。-t
文件句柄打开到一个 tty。-u
文件已设置 setuid 位。-g
文件已设置 setgid 位。-k
文件设置了粘性位。-T
文件是 ASCII 或 UTF-8 文本文件(启发式猜测)。-B
File 是一个“二进制”文件(与-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(-e $base_path)
【讨论】:
-f 来测试普通文件(而不是目录或各种其他特殊类型的文件)
用途:
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;
【讨论】: