Python and FTP

Trying to download image, just makes blank file

Page 1 of 1

9 Replies - 1685 Views - Last Post: 28 May 2010 - 07:01 AM Rate Topic: -----

#1 Kacie  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 01-November 06

Python and FTP

Posted 26 May 2010 - 07:38 PM

So I have worked on this all damn day long. I stopped around 5:30 but now I need some help. I just get a blank file here is a simplified version of what I am doing, when I run this code I get the same error as the real code that needs to run:

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)
send_image = open("015693PR-com.jpg", 'r')



So the image is there, I can look at it, but when I open the file that this code makes, it is just blank. I tried to look around the urllib and urllib2 and played with that, but I can't figure out how to use it with a login using FTP. I have tried A LOT of stuff. I tried using storlines and still nothing in the file. I am ready to pull my hair out.

This post has been edited by Kacie: 26 May 2010 - 07:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Python and FTP

#2 supersssweety  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 22
  • View blog
  • Posts: 373
  • Joined: 16-March 07

Re: Python and FTP

Posted 26 May 2010 - 07:40 PM

This was actually me, I didn't know I had two logins, one w/ my username and one w/ my email that is the same as my username...ok...not a new dic head up there ^

This post has been edited by supersssweety: 26 May 2010 - 07:40 PM

Was This Post Helpful? 0
  • +
  • -

#3 supersssweety  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 22
  • View blog
  • Posts: 373
  • Joined: 16-March 07

Re: Python and FTP

Posted 26 May 2010 - 08:09 PM

OK so I have changed the line:
notions_ftp.retrlines("RETR 015693PR-com.jpg", open("015693PR-com.jpg","wb").write)


...and I actually see some FTP output:

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


250 I always saw but 226 is new...and disappointing :(
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5642
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Python and FTP

Posted 27 May 2010 - 04:22 AM

The method retrlines is the wrong choice. Look at retrbinary instead.
Was This Post Helpful? 0
  • +
  • -

#5 supersssweety  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 22
  • View blog
  • Posts: 373
  • Joined: 16-March 07

Re: Python and FTP

Posted 27 May 2010 - 07:55 AM

Thanks but it gives the same results :(
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5642
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Python and FTP

Posted 27 May 2010 - 08:44 AM

You're passing a file handle, not a function for the callback. Try this:
notions_ftp = ftplib.FTP(ftp_host, ftp_user, ftp_passwd)
notions_ftp.cwd("Leisure Arts - Images")

# ok, you have ha file handle
image = open("015693PR-com.jpg", "wb") 

# passing just the file handle is wrong, it's looking for a callback function
# notions_ftp.retrlines("RETR 015693PR-com.jpg", image)
# this should work
notions_ftp.retrbinary("RETR 015693PR-com.jpg", image.write)



If you're going to always save with the same filename your retrieved, write a function:


# this will also be cleaner
def saveFile(client, filename):
	fh = open(filename, 'wb')
	cb = fh.write
	client.retrbinary('RETR %s' % filename, cb)
	fh.close()

notions_ftp = ftplib.FTP(ftp_host, ftp_user, ftp_passwd)
notions_ftp.cwd("Leisure Arts - Images")
saveFile(notions_ftp, "015693PR-com.jpg")



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#7 supersssweety  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 22
  • View blog
  • Posts: 373
  • Joined: 16-March 07

Re: Python and FTP

Posted 27 May 2010 - 10:05 AM

:) I had tried just .write also, but I went ahead and tried again, still doesn't transfer anything. 226 Transfer complete. 0 bytes in 0.00 sec. (0.000 Kb/s) Turns out these images are super uber huge, is there a limit that can be transferred?
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5642
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Python and FTP

Posted 27 May 2010 - 01:12 PM

Size shouldn't really be an issue. You will get a zero byte file if it failed to find the file.

Pay attention to your error messages. You can also write some custom stuff to track what's going on.

The following will print out each time a block is processed. It's also written so that no file will be created if the FTP retrbinary fails to start.
def getFtpFile(ftp_host, ftp_user, ftp_passwd, dirname, filename):
	def retr(client):
		retr.count = 0
		retr.fh = None
		def customCallback(data):
			if retr.fh==None:
				retr.fh = open(filename, 'wb')
			retr.count += len(data)
			print "#", retr.count
			retr.fh.write(data)
			
		client.retrbinary('RETR %s' % filename, customCallback)
		if retr.fh!=None:
			retr.fh.close()
	
	client = ftplib.FTP(ftp_host, ftp_user, ftp_passwd)
	client.cwd(dirname)
	# client.retrlines('LIST') # uncomment this to see where you are
	retr(client)
	client.quit()


Was This Post Helpful? 0
  • +
  • -

#9 supersssweety  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 22
  • View blog
  • Posts: 373
  • Joined: 16-March 07

Re: Python and FTP

Posted 27 May 2010 - 02:14 PM

I have moved onto something else for this work day, but I will most def try this out tonight and let you know how it goes, I refuse to be defeated! Of course by now one of the web designers could have downloaded, re-sized, and uploaded all of these pictures to our site by now as there are only 300 :(
Was This Post Helpful? 0
  • +
  • -

#10 supersssweety  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 22
  • View blog
  • Posts: 373
  • Joined: 16-March 07

Re: Python and FTP

Posted 28 May 2010 - 07:01 AM

It never did print anything when I ran it this way, and when I un-commented out the LIST line, it listed the file I was trying to process :/ *sigh* I think it just hates me.

This post has been edited by supersssweety: 28 May 2010 - 07:07 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1