开发者

Python FTP grabbing and saving images issue

开发者 https://www.devze.com 2023-01-01 17:21 出处:网络
EDIT: I got it working it just won\'t download anything... So here is my code simplified now: notions_ftp = ftplib.FTP(ftp_host, ftp_user, ftp_passwd)

EDIT: I got it working it just won't download anything...

So here is my code simplified now:

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')

And Here is my output:

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

Original Post: OK So I have been messing with this all day long. I am fairly new to Python FTP. So I have searched through here and came up w/ this:

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

So I have narrowed the error down to the line before I divide by zero. Here is the error:

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'

So if you follow the code you will see that the image IS in the directory because image_name_we_want is set if found in that directory listing on the first line of my code. And I KNOW it's there because I am looking at the FTP site myself and ...it's freakin there. So at some point during all of this I got the image to save locally, which is most desired, but I have long since forgot what I used to make it do that. Either way, why does it think that the image isn't there when it clearly finds it in the listing.

OK so I took the 开发者_如何学Csuggestion and now I get:

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

So it got the first image, which I think really is corrupt, but then when it goes on to the next one to be processed it throws that error on the file object. So here is how I changed it and the rest of the code not put here in the first place:

# 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


You're reading the file instead of writing it.

So instead of open(image_name_we_want, "rb") use open(image_name_we_want, "wb")

[edit] If you're simply fetching from an ftp server than you could also try this:

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

Also, for a fully working example read this: http://docs.python.org/library/ftplib.html

You're also missing the write after the open()

0

精彩评论

暂无评论...
验证码 换一张
取 消