首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何检查Perl中是否存在文件?

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

Stack Overflow用户
提问于 2010-04-08 23:09:10
回答 7查看 290.7K关注 0票数 119

我有一个相对路径

代码语言:javascript
复制
   $base_path = "input/myMock.TGZ";

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

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

EN

回答 7

Stack Overflow用户

发布于 2010-04-08 23:42:01

你可能想要一个exists的变种。perldoc -f "-f“

代码语言:javascript
复制
      -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.
票数 30
EN

Stack Overflow用户

发布于 2010-04-08 23:10:20

代码语言:javascript
复制
if (-e $base_path)
{ 
 # code
}

-e是Perl中的“existence”运算符。

您可以使用this page上的代码检查权限和其他属性。

票数 18
EN

Stack Overflow用户

发布于 2010-04-08 23:11:27

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

票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2601027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档