测试文件是否不是 Perl 中的目录

发布于 2024-12-09 16:31:31 字数 95 浏览 1 评论 0原文

我知道您可以使用以下命令测试文件是否是目录:

if(-d $filename)

但是如何测试它是否不是目录?

I'm aware you can test if a file is a directory using:

if(-d $filename)

but how can you test if it's not a directory?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

合约呢 2024-12-16 16:31:31

您是否想过尝试以下方法?

if (! -d $filename) ...

毕竟,-d 的结果可以被视为布尔值,因此逻辑非运算符 ! 可以正常工作。

而且,如果您想要比“不是目录”更具体的内容,请参阅这里。有很多东西不是目录,有时将它们视为常规文件是没有意义的。

请记住,这是假设您已经验证文件名存在。如果您执行 ! -d 对于不存在的文件名,它将返回 true。

你是否认为一个不存在的东西“不是目录”是一个哲学问题,但是,如果你想确保它也存在,你可以使用 ! -d-e 结合使用,例如:

if ((-e $filename) && (! -d $filename)) ...

Have you thought of trying the following?

if (! -d $filename) ...

The result of -d is, after all, something that can be treated as a boolean value, hence the logical not operator ! will work fine.

And, if you're after something more specific than "not a directory", see here. There are quite a few things that aren't directories that it sometimes doesn't make sense to treat as regular files.

Keep in mind that's assuming you've already validated that the filename exists. If you do a ! -d on a non-existent filename, it will return true.

It's a philosophical matter as to whether you consider a non-existent thing to be "not a directory" but, if you want to ensure it exists as well, you can use ! -d in conjunction with -e, something like:

if ((-e $filename) && (! -d $filename)) ...
笑咖 2024-12-16 16:31:31

要测试某物是否是文件,而不是目录,您需要简单地组合 2 个测试(没有单个测试):

if (-e $file && !-d $file) { # a file but not a directory }

请注意:

  • 恕我直言,paxdiablo 的答案对于问题的方式是错误的措辞(纯 !-d 不适用于用户询问的内容,因为它检查随机事物/字符串是否不是目录,而不是某事物是否是不是目录的文件)。例如 !-d NO_SUCH_FILE 返回 true。

  • Greg 的答案可能是正确的,完全取决于原始用户是否打算在“不是目录的文件”的定义中包含非普通文件(例如符号链接、命名管道等)。如果他们打算包含所有这些特殊的“文件”,那么格雷格的答案也是错误的,因为“-f”排除了它们(正如格雷格首先在他的回答中明智地指出的那样)。

To test if something is a file, BUT not a directory, you need to simply combine the 2 tests (there's no single test):

if (-e $file && !-d $file) { # a file but not a directory }

Please note that:

  • With all due respect, paxdiablo's answer is wrong for the way the question is worded ( pure !-d does not work for what the user asked as it checks if a random thing/string is not a directory, NOT whether something is a file which is not a directory). E.g. !-d NO_SUCH_FILE returns true.

  • Greg's answer may be correct depending entirely on whether the original user meant to include non-plain files (e.g. symbolic links, named pipes, etc..) in their definition of "file which is not a directory". If they meant to include all those special "files", Greg's answer is also wrong as "-f" excludes them (as Greg wisely noted in his answer in the first place).

⊕婉儿 2024-12-16 16:31:31

本着 TIMTOWTDI 的精神:

unless (-d $filename)

In the spirit of TIMTOWTDI:

unless (-d $filename)
瀟灑尐姊 2024-12-16 16:31:31

-f 运算符测试某些内容是否是“常规文件”。这不包括目录,但也不包括管道和设备节点之类的东西。

if (-f $filename) { ...

The -f operator tests to see whether something is a "regular file". This excludes directories, but also excludes such things as pipes and device nodes.

if (-f $filename) { ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文