gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 04:43:38 25 4
gpt4 key购买 nike

我有一个相对路径

   $base_path = "input/myMock.TGZ";

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

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

最佳答案

使用 -e 文件测试运算符测试给定路径中是​​否存在某些东西

print "$base_path exists!\n" if -e $base_path;

但是,此测试可能比您预期的更广泛。如果该路径中存在普通文件,上面的代码将生成输出,但它也会针对目录、命名管道、符号链接(symbolic link)或更奇特的可能性触发。 See the documentation了解详情。

鉴于您问题中的 .TGZ 扩展名,您似乎期望一个纯文件而不是替代文件。 -f 文件测试运算符询问路径是否通向普通文件。

print "$base_path is a plain file!\n" if -f $base_path;

perlfunc 文档涵盖了长长的列表 Perl's file-test operators这涵盖了您在实践中会遇到的许多情况。

  • -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 (false if symlinks aren’t supported by the file system).
  • -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 or UTF-8 text file (heuristic guess).
  • -B
    File is a “binary” file (opposite of -T).
  • -M
    Script start time minus file modification time, in days.
  • -A
    Same for access time.
  • -C
    Same for inode change time (Unix, may differ for other platforms)

关于perl - Perl 中如何检查文件是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2601027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com