[perl] Perl에 파일이 있는지 어떻게 확인할 수 있습니까?

나는 상대 경로가 있습니다

   $base_path = "input/myMock.TGZ";

myMock.TGZ입력 폴더에있는 파일 이름입니다. 파일 이름은 변경 될 수 있습니다. 그러나 경로는 항상 $base_path.

파일이 .NET에 있는지 확인해야합니다 $base_path.



답변

file-test 연산자를 사용하여 주어진 경로에 무언가 가 있는지 테스트합니다 -e.

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

그러나이 테스트는 의도 한 것보다 더 광범위 할 수 있습니다. 위의 코드는 해당 경로에 일반 파일이있는 경우 출력을 생성하지만 디렉토리, 명명 된 파이프, 심볼릭 링크 또는 좀 더 이국적인 가능성에 대해서도 실행됩니다. 자세한 내용은 설명서 를 참조하십시오.

.TGZ질문 의 확장자를 감안할 때 대안이 아닌 일반 파일 을 기대하는 것 같습니다 . -f파일 테스트 연산자는 경로가 일반 파일로 연결 여부를 묻습니다.

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

perlfunc 문서는 실제로 직면하게 될 많은 상황을 다루는 Perl의 파일 테스트 연산자의 긴 목록 을 다룹니다.

  • -r
    유효한 uid / gid로 파일을 읽을 수 있습니다.
  • -w
    유효한 uid / gid로 파일을 쓸 수 있습니다.
  • -x
    유효한 uid / gid로 파일을 실행할 수 있습니다.
  • -o
    유효 UID가 파일을 소유합니다.
  • -R
    실제 uid / gid로 파일을 읽을 수 있습니다.
  • -W
    실제 uid / gid로 파일을 쓸 수 있습니다.
  • -X
    파일은 실제 uid / gid로 실행 가능합니다.
  • -O
    파일은 실제 uid가 소유합니다.
  • -e
    파일이 존재.
  • -z
    파일 크기가 0입니다 (비어 있음).
  • -s
    파일 크기가 0이 아닙니다 (바이트 단위로 크기 반환).
  • -f
    파일은 일반 파일입니다.
  • -d
    파일은 디렉토리입니다.
  • -l
    파일이 심볼릭 링크입니다 (파일 시스템에서 심볼릭 링크를 지원하지 않는 경우 false).
  • -p
    파일이 명명 된 파이프 (FIFO)이거나 파일 핸들이 파이프입니다.
  • -S
    파일이 소켓입니다.
  • -b
    파일은 블록 특수 파일입니다.
  • -c
    파일은 문자 특수 파일입니다.
  • -t
    tty에 대해 파일 핸들이 열립니다.
  • -u
    파일에 setuid 비트가 설정되었습니다.
  • -g
    파일에 setgid 비트가 설정되었습니다.
  • -k
    파일에 고정 비트가 설정되었습니다.
  • -T
    파일이 ASCII 또는 UTF-8 텍스트 파일입니다 (휴리스틱 추측).
  • -B
    파일이 “이진”파일입니다 (의 반대 -T).
  • -M
    스크립트 시작 시간에서 파일 수정 시간 (일)을 뺀 값입니다.
  • -A
    액세스 시간도 동일합니다.
  • -C
    inode 변경 시간과 동일 (Unix, 다른 플랫폼에서는 다를 수 있음)

답변

존재하는 변형을 원할 수 있습니다 … 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
               doesnt 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)
{
 # code
}

-e Perl의 ‘존재’연산자입니다.

이 페이지 의 코드를 사용하여 권한 및 기타 속성을 확인할 수 있습니다 .


답변

당신이 사용할 수있는: if(-e $base_path)


답변

사용하다:

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";}

트릭을 할 것이다