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

时间:2022-12-04 08:29:43

I have a relative path

我有一个相对路径。

   $base_path = "input/myMock.TGZ";

myMock.TGZ is the file name located in input folder. The filename can change. But the path is always stored in $base_path.

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

I need to check if the file exists in $base_path.

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

8 个解决方案

#1


130  

Test whether something exists at given path using the -e file-test operator.

测试使用-e文件测试操作符是否存在给定路径。

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

However, this test is probably broader than you intend. The code above will generate output if a plain file exists at that path, but it will also fire for a directory, a named pipe, a symlink, or a more exotic possibility. See the documentation for details.

但是,这个测试可能比您想的要广泛。如果在该路径中存在一个简单的文件,上面的代码将生成输出,但是它也会触发一个目录,一个命名管道,一个符号链接,或者一个更奇异的可能性。有关详细信息,请参阅文档。

Given the extension of .TGZ in your question, it seems that you expect a plain file rather than the alternatives. The -f file-test operator asks whether a path leads to a plain file.

在您的问题中,考虑到. tgz的扩展,似乎您期望的是一个普通文件,而不是其他选项。-f文件测试操作符询问一条路径是否会导致一个普通文件。

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

The perlfunc documentation covers the long list of Perl's file-test operators that covers many situations you will encounter in practice.

perlfunc文档涵盖了许多Perl文件测试操作符,它们涵盖了在实践中遇到的许多情况。

  • -r
    File is readable by effective uid/gid.
  • -r文件通过有效的uid/gid可读。
  • -w
    File is writable by effective uid/gid.
  • -w文件通过有效的uid/gid可写。
  • -x
    File is executable by effective uid/gid.
  • -x文件由有效的uid/gid执行。
  • -o
    File is owned by effective uid.
  • -o文件归有效uid所有。
  • -R
    File is readable by real uid/gid.
  • -R文件可通过真实uid/gid读取。
  • -W
    File is writable by real uid/gid.
  • -W文件是由真实uid/gid编写的。
  • -X
    File is executable by real uid/gid.
  • -X文件是由真正的uid/gid执行的。
  • -O
    File is owned by real uid.
  • -O文件属于真实uid。
  • -e
    File exists.
  • - e文件存在。
  • -z
    File has zero size (is empty).
  • -z文件的大小为零(为空)。
  • -s
    File has nonzero size (returns size in bytes).
  • -s文件有非零大小(以字节为单位返回大小)。
  • -f
    File is a plain file.
  • -f文件是一个普通文件。
  • -d
    File is a directory.
  • -d文件是一个目录。
  • -l
    File is a symbolic link (false if symlinks aren’t supported by the file system).
  • -l文件是一个符号链接(如果文件系统不支持符号链接)。
  • -p
    File is a named pipe (FIFO), or Filehandle is a pipe.
  • -p文件是一个命名管道(FIFO),或Filehandle是一个管道。
  • -S
    File is a socket.
  • -S文件是一个套接字。
  • -b
    File is a block special file.
  • -b文件是块特殊文件。
  • -c
    File is a character special file.
  • -c文件是一个字符特殊文件。
  • -t
    Filehandle is opened to a tty.
  • - tfilehandle被打开到一个tty。
  • -u
    File has setuid bit set.
  • -u文件有setuid位集。
  • -g
    File has setgid bit set.
  • -g文件设置了setgid位。
  • -k
    File has sticky bit set.
  • -k文件有粘性的位集。
  • -T
    File is an ASCII or UTF-8 text file (heuristic guess).
  • -T文件是一个ASCII或UTF-8文本文件(启发式猜测)。
  • -B
    File is a “binary” file (opposite of -T).
  • -B文件是一个“二进制”文件(与-T相反)。
  • -M
    Script start time minus file modification time, in days.
  • -M脚本开始时间减去文件修改时间,在几天内。
  • -A
    Same for access time.
  • 对于访问时间也是一样的。
  • -C
    Same for inode change time (Unix, may differ for other platforms)
  • -C对于inode更改时间相同(Unix,对于其他平台可能不同)

#2


29  

You might want a variant of exists ... perldoc -f "-f"

你可能想要一个变体的存在…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.

#3


14  

if (-e $base_path)
{ 
 # code
}

-e is the 'existence' operator in Perl.

-e是Perl中的“存在”操作符。

You can check permissions and other attributes using the code on this page.

您可以使用此页面上的代码检查权限和其他属性。

#4


12  

You can use: if(-e $base_path)

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

#5


9  

Use:

使用:

if (-f $filePath)
{
  # code
}

-e returns true even if the file is a directory. -f will only return true if it's an actual file

-e返回true,即使该文件是一个目录。-f只返回true,如果它是一个实际的文件。

#6


5  

#!/usr/bin/perl -w

$fileToLocate = '/whatever/path/for/file/you/are/searching/MyFile.txt';
if (-e $fileToLocate) {
    print "File is present";
}

#7


3  

if(-e $base_path){print "Something";}

would do the trick

就能解决问题

#8


0  

Use the below code. Here -f checks, it's a file or not:

使用以下代码。这里-f检查,它是一个文件

print "File $base_path is exists!\n" if -f $base_path;

and enjoy

和享受

#1


130  

Test whether something exists at given path using the -e file-test operator.

测试使用-e文件测试操作符是否存在给定路径。

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

However, this test is probably broader than you intend. The code above will generate output if a plain file exists at that path, but it will also fire for a directory, a named pipe, a symlink, or a more exotic possibility. See the documentation for details.

但是,这个测试可能比您想的要广泛。如果在该路径中存在一个简单的文件,上面的代码将生成输出,但是它也会触发一个目录,一个命名管道,一个符号链接,或者一个更奇异的可能性。有关详细信息,请参阅文档。

Given the extension of .TGZ in your question, it seems that you expect a plain file rather than the alternatives. The -f file-test operator asks whether a path leads to a plain file.

在您的问题中,考虑到. tgz的扩展,似乎您期望的是一个普通文件,而不是其他选项。-f文件测试操作符询问一条路径是否会导致一个普通文件。

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

The perlfunc documentation covers the long list of Perl's file-test operators that covers many situations you will encounter in practice.

perlfunc文档涵盖了许多Perl文件测试操作符,它们涵盖了在实践中遇到的许多情况。

  • -r
    File is readable by effective uid/gid.
  • -r文件通过有效的uid/gid可读。
  • -w
    File is writable by effective uid/gid.
  • -w文件通过有效的uid/gid可写。
  • -x
    File is executable by effective uid/gid.
  • -x文件由有效的uid/gid执行。
  • -o
    File is owned by effective uid.
  • -o文件归有效uid所有。
  • -R
    File is readable by real uid/gid.
  • -R文件可通过真实uid/gid读取。
  • -W
    File is writable by real uid/gid.
  • -W文件是由真实uid/gid编写的。
  • -X
    File is executable by real uid/gid.
  • -X文件是由真正的uid/gid执行的。
  • -O
    File is owned by real uid.
  • -O文件属于真实uid。
  • -e
    File exists.
  • - e文件存在。
  • -z
    File has zero size (is empty).
  • -z文件的大小为零(为空)。
  • -s
    File has nonzero size (returns size in bytes).
  • -s文件有非零大小(以字节为单位返回大小)。
  • -f
    File is a plain file.
  • -f文件是一个普通文件。
  • -d
    File is a directory.
  • -d文件是一个目录。
  • -l
    File is a symbolic link (false if symlinks aren’t supported by the file system).
  • -l文件是一个符号链接(如果文件系统不支持符号链接)。
  • -p
    File is a named pipe (FIFO), or Filehandle is a pipe.
  • -p文件是一个命名管道(FIFO),或Filehandle是一个管道。
  • -S
    File is a socket.
  • -S文件是一个套接字。
  • -b
    File is a block special file.
  • -b文件是块特殊文件。
  • -c
    File is a character special file.
  • -c文件是一个字符特殊文件。
  • -t
    Filehandle is opened to a tty.
  • - tfilehandle被打开到一个tty。
  • -u
    File has setuid bit set.
  • -u文件有setuid位集。
  • -g
    File has setgid bit set.
  • -g文件设置了setgid位。
  • -k
    File has sticky bit set.
  • -k文件有粘性的位集。
  • -T
    File is an ASCII or UTF-8 text file (heuristic guess).
  • -T文件是一个ASCII或UTF-8文本文件(启发式猜测)。
  • -B
    File is a “binary” file (opposite of -T).
  • -B文件是一个“二进制”文件(与-T相反)。
  • -M
    Script start time minus file modification time, in days.
  • -M脚本开始时间减去文件修改时间,在几天内。
  • -A
    Same for access time.
  • 对于访问时间也是一样的。
  • -C
    Same for inode change time (Unix, may differ for other platforms)
  • -C对于inode更改时间相同(Unix,对于其他平台可能不同)

#2


29  

You might want a variant of exists ... perldoc -f "-f"

你可能想要一个变体的存在…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.

#3


14  

if (-e $base_path)
{ 
 # code
}

-e is the 'existence' operator in Perl.

-e是Perl中的“存在”操作符。

You can check permissions and other attributes using the code on this page.

您可以使用此页面上的代码检查权限和其他属性。

#4


12  

You can use: if(-e $base_path)

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

#5


9  

Use:

使用:

if (-f $filePath)
{
  # code
}

-e returns true even if the file is a directory. -f will only return true if it's an actual file

-e返回true,即使该文件是一个目录。-f只返回true,如果它是一个实际的文件。

#6


5  

#!/usr/bin/perl -w

$fileToLocate = '/whatever/path/for/file/you/are/searching/MyFile.txt';
if (-e $fileToLocate) {
    print "File is present";
}

#7


3  

if(-e $base_path){print "Something";}

would do the trick

就能解决问题

#8


0  

Use the below code. Here -f checks, it's a file or not:

使用以下代码。这里-f检查,它是一个文件

print "File $base_path is exists!\n" if -f $base_path;

and enjoy

和享受