Python FTP抓取并保存图像问题

时间:2010-05-26 18:14:55

标签: python image ftp

编辑:我开始工作它不会下载任何东西......

所以这是我现在简化的代码:

notions_ftp = ftplib.FTP(ftp_host, ftp_user, ftp_passwd)
folder = "Leisure Arts - Images"
notions_ftp.cwd(folder)
image = open("015693PR-com.jpg","wb")
notions_ftp.retrlines("RETR 015693PR-com.jpg", image.write)
send_image = open("015693PR-com.jpg", 'r')

这是我的输出:

'250 "/Leisure Arts - Images": is current directory.'
'226 Transfer complete. 0 bytes in 0.00 sec. (0.000 Kb/s)'

原帖: 好吧所以我一整天都在搞乱这件事。我是Python FTP的新手。所以我在这里搜索了一下,然后来了:

images = notions_ftp.nlst()
    for image_name in image_names:
        if found_url == False:
            try:
                for image in images:
                    ftp_image_name = "./%s" % image_name
                    if ftp_image_name == image:
                        found_url = True
                        image_name_we_want = image_name
            except:
                pass

    # We failed to find an image for this product, it will have to be done manually
    if found_url == False:
        log.info("Image ain't there baby -- SKU: %s" % sku)
        return False
    # Hey we found something! Open the image....
    notions_ftp.retrlines("RETR %s" % image_name_we_want, open(image_name_we_want, "rb"))
    1/0

因此,在将除以零之前,我已将误差缩小到直线。这是错误:

Traceback (most recent call last):
  File "<console>", line 6, in <module>
  File "<console>", line 39, in insert_image
IOError: [Errno 2] No such file or directory: '411483CC-IT,IM.jpg'

因此,如果您按照代码操作,您将看到目录中的图像IS,因为如果在我的代码第一行的目录列表中找到了image_name_we_want,则会设置该图像。而且我知道它就在那里,因为我自己正在看FTP网站......那里很奇怪。因此,在所有这些过程中的某些时刻,我得到的图像是本地保存的,这是最理想的,但我早就忘记了我曾经做过的事情。无论哪种方式,为什么当它在清单中清楚地找到它时,它认为图像不存在。

好的,所以我接受了这个建议,现在我得到了:

14:01:18,057 ERROR [pylons-admin] Image to big or corrupt! Skipping Image for product sku: 411483
14:01:18,057 ERROR [pylons-admin] Image to big or corrupt! Skipping Image for product sku: 411483
Traceback (most recent call last):
  File "<console>", line 6, in <module>
  File "<console>", line 40, in insert_image
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/ftplib.py", line 418, in retrlines
    callback(line)
TypeError: 'file' object is not callable
>>> 14:01:24,694 INFO  [pylons-admin] Script Done! Updated 0 Products w/ Images

所以它得到了第一个图像,我认为它确实已损坏,但是当它继续处理下一个要处理的图像时,它会在文件对象上抛出该错误。所以这就是我如何改变它以及其他未放在首位的代码:

# Hey we found something! Open the image....
    f = open(image_name_we_want, "wb")
    notions_ftp.retrlines("RETR %s" % image_name_we_want, f)
    send_image = open(image_name_we_want, 'r')

    # ...and send it for processing
    try:
        image_id = product_obj.set_image(send_image, 5, 1)
    except IOError, error:
        log.error("Image to big or corrupt! Skipping Image for product sku: %s" % sku)
        image_id = False
    else:
        if image_id == False:
            log.error("Could not Insert the image for product sku: %s" % sku)
            f.close()
            return False
        else:
            f.close()
            os.remove(image_name_we_want)
            return True

1 个答案:

答案 0 :(得分:1)

您正在阅读该文件而不是编写文件。

而不是open(image_name_we_want, "rb")使用open(image_name_we_want, "wb")

[编辑] 如果您只是从ftp服务器获取,那么您也可以尝试:

import urllib2
fh = urllib2.urlopen('ftp://server/path/file.png')
file('file.png', 'wb').write(fh.read())

另外,对于一个完整的工作示例,请阅读:http://docs.python.org/library/ftplib.html

您在write

之后也错过了open()