tôi đang sử dụng SFTPClient
để tải xuống tệp từ máy chủ từ xa. Nhưng tôi không thể biết con đường từ xa là một tập tin hoặc một directoty. nếu đường dẫn từ xa là một thư mục tôi cần để xử lý thư mục này đệ quy.Cách kiểm tra đường dẫn từ xa là một tệp hoặc một thư mục?
đây là mã của tôi:
def downLoadFile(sftp, remotePath, localPath):
for file in sftp.listdir(remotePath):
if os.path.isfile(os.path.join(remotePath, file)): # file, just get
try:
sftp.get(file, os.path.join(localPath, file))
except:
pass
elif os.path.isdir(os.path.join(remotePath, file)): # dir, need to handle recursive
os.mkdir(os.path.join(localPath, file))
downLoadFile(sftp, os.path.join(remotePath, file), os.path.join(localPath, file))
if __name__ == '__main__':
paramiko.util.log_to_file('demo_sftp.log')
t = paramiko.Transport((hostname, port))
t.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(t)
tôi tìm ra vấn đề: chức năng os.path.isfile
hoặc os.path.isdir
trở False
, vì vậy tôi nghĩ rằng những chức năng cann't làm việc cho remotePath.
kiểm tra mở rộng đường dẫn nếu có phần mở rộng hoặc. – SANDEEP
Tệp không nhất thiết phải có phần mở rộng. – Noctua
có thể trùng lặp của [sử dụng st \ _mode để xác định tệp hoặc thư mục] (http://stackoverflow.com/questions/15861967/using-st-mode-to-identify-file-or-directory) –