본문 바로가기

시행착오/[python]

[python] 로컬 및 원격 서버내 파일/디렉토리 존재 확인법

[로컬]

isdir isfile

import os
print(os.path.isdir("/home/temp"))
print(os.path.isfile("/home/temp/test.txt"))

technote.kr/207

[원격]

import errno
import os

def exists_remote(sftp_client, path):
    try:
        sftp_client.stat(path)
    except IOError as e:
        if e.errno == errno.ENOENT:
            return False
	raise        
    else:
        return True

stackoverflow.com/questions/4409502/directory-transfers-with-paramiko

docs.python.org/ko/3/library/errno.html

 

원격 경로가 파일인지 디렉토리인직 구분

import stat

def downLoadFile(sftp, remotePath, localPath):
    for fileattr in sftp.listdir_attr(remotePath):  
        if stat.S_ISDIR(fileattr.st_mode):
            sftp.get(fileattr.filename, os.path.join(localPath, fileattr.filename))

stackoverflow.com/questions/18205731/how-to-check-a-remote-path-is-a-file-or-a-directory