PySFTP不总是放文件,这是一个编码问题吗?

2024-05-19 12:24:02 发布

您现在位置:Python中文网/ 问答频道 /正文

所以最近我写了一个代码,将文件从一个文件夹放到一个SFTP文件夹。它一般工作,但很多时候它无法上传数据,我没有线索的方法。你有什么建议吗

import os
import pysftp

cnopts = pysftp.CnOpts()
cnopts.hostkeys = None 

#Set these to make the code run
shared_aim_fd='' #the folder where you copy from
sftp_user=''
sftp_pw=''
sftp_site=''
sftp_folder_name='' #where you want to copy to

for od_files in os.listdir(shared_aim_fd):
    myUsername =str(sftp_user) 
    myPassword =str(sftp_pw)
    myHostname=str(sftp_site)
    mySFTPFolderName=str(sftp_folder_name)

    try:
        with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword, cnopts=cnopts) as sftp:
            print ("Connection succesfully stablished ... ")
            #Make sure we have the folder where we want to push it
            if not sftp.isdir(mySFTPFolderName): 
                #if not, then create one 
                sftp.makedirs(mySFTPFolderName)
            sftp.put(shared_aim_fd+od_files,str(mySFTPFolderName+'/'+od_files))
            sftp.close()

    except:
        print("Upload failed")
        pass

Tags: theto文件夹filesfolderwheresharedsftp