FTP中的FTP上传问题 [英] FTP upload issue in Python

查看:181
本文介绍了FTP中的FTP上传问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Python 2.7将文件上传到远程服务器,但是当我运行脚本时,远程FTP服务器上的文件始终被称为 test_0test _ 远程FTP服务器,而不是 test_0_13.pic.jpg ,如我所料。任何想法?

  from ftplib import FTP 

hosts = [('1.2.3.4',' admin','12345')]
local_file = r'/ Users / foo / Downloads / 13.pic.jpg'
remote_file_base_name_prefix ='test_'
counter = 0
remote_file_base_name_suffix ='_ 13.pic.jpg'

主机,名称,密码在主机中:
f = FTP(主机,名称,密码)
f.cwd('输入')

打开remote_file_base_name_prefix + str(counter)+ remote_file_base_name_suffix
$ b $打开(local_file,'rb')作为f_local:
f.storbinary('STOR {}' .format(remote_file_base_name_prefix + str(counter)+ remote_file_base_name_prefix),f_local)

print{} - done.format(host)
f.quit()

预先感谢,
Lin

解决方案

  f.storbinary('STOR {} 

.format(remote_file_base_name_prefix + STR (计数器)+ remote_file_base_name_prefix),f_local)

应该是

  f.storbinary('STOR {}'.format(remote_file_base_name_prefix + str(counter)+ remote_file_base_name_suffix),f_local)


I am trying to upload a file to a remote server using Python 2.7, but when I run the script, the file on the remote FTP server is always called test_0test_ on remote FTP server, rather than test_0_13.pic.jpg as I would expect. Any ideas?

from ftplib import FTP

hosts = [('1.2.3.4', 'admin', '12345')]
local_file = r'/Users/foo/Downloads/13.pic.jpg'
remote_file_base_name_prefix = 'test_'
counter = 0
remote_file_base_name_suffix='_13.pic.jpg'

for host, name, password in hosts:
    f = FTP(host, name, password)
    f.cwd('Input')

    print remote_file_base_name_prefix+str(counter)+remote_file_base_name_suffix

    with open(local_file, 'rb') as f_local:
        f.storbinary('STOR {}'.format(remote_file_base_name_prefix+str(counter)+remote_file_base_name_prefix), f_local)

    print "{} - done".format(host)
    f.quit()

thanks in advance, Lin

解决方案

Ah, you just had a typo in your code:

f.storbinary('STOR {}'.format(remote_file_base_name_prefix+str(counter)+remote_file_base_name_prefix), f_local)

should be

f.storbinary('STOR {}'.format(remote_file_base_name_prefix+str(counter)+remote_file_base_name_suffix), f_local)

这篇关于FTP中的FTP上传问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆