在python中使用ftplib上传文件

时间:2019-02-12 05:36:42

标签: python ftplib

使用“ ftplib” Python软件包,我连接到主机,然后使用“ storbinary:函数”将二进制文件发送到该目录。但是,文件传输不会发生,源文件被覆盖并制成0个字节

在上传文件之前,尝试使用ftplib中的“ cwd”更改为目标目录。 在命令行上尝试了“ rsync”,效果很好(从而建立了文件完整性)。

print ('Establishing FTP connection')
ftp = FTP(dest_dir, 'user', 'password')
print '+++++', ftp.pwd()   
ftp.cwd(dest_dir)
print 'work dir now', ftp.pwd() 
ftp.retrlines('LIST') 
f_name = /home/test/file_to_upload
with open(f_name, 'rb') as cfile:
    ftp.storbinary('STOR %s' % f_name, cfile)
ftp.quit()

文件:/ home / test / file_to_upload应该上传到dest_dir,而不是被覆盖并且也变成空文件,即文件内容被删除

1 个答案:

答案 0 :(得分:0)

f_name包含本地系统的目录。尝试仅提取文件名。另外请注意在“ STOR”和“ filename”之间添加空格

示例:

ftp.storbinary('STOR '+ os.path.basename(f_name), open(f_name, 'rb'))
相关问题