Question

I've written some python code to download an image using

urllib.urlopen().read()

and then upload it to an FTP site using

ftplib.FTP().storbinary()

but I'm having a problem. Sometimes the image file is only partially uploaded, so I get images with the bottom 20% or so cut off. I've checked the locally downloaded version and I have successfully downloaded the entire image, which leads me to believe that it is a problem with storbinary. I believe I am opening and closing all of the files correctly. Does anyone have any clues as to why I'm getting a partial upload with storbinary?

Update: When I run through the commands in the Python shell, the upload completes successfully, I don't know why it would be different from when run as a script...

Was it helpful?

Solution 2

It's been a while since I looked at this code, but I remember the crux of it was that I was not closing the downloaded file correctly. I have the working code though, so just in case it was a problem with the upload and not the download, here are both snippets:

Here is the working code to download the image:

socket = urllib.urlopen(TheURL)
FileContents = socket.read()
LocalFilename = LocalDir + FilenameOnly
LocalFile = open(LocalDir + FilenameOnly, 'wb')
LocalFile.write(FileContents)
LocalFile.close()

Where TheURL is the URL of the file I'm trying to download, FilenameOnly is just the filename portion of the path, and LocalDir is the local destination. I believe my problem was that I was not calling LocalFile.close().

Here is the working code to upload the image:

FTPServer = ftplib.FTP(FTPServer, FTPUsername, FTPPassword)
UploadFile = open(Filename, "rb")
FTPServer.cwd(FTPSubDirectory)

UploadFile.close()
FTPServer.quit()

The problem could also have been that I was not calling FTPServer.quit()

If anyone has any questions about this code, I'll happily reply in the comments; I feel really bad that I left any Googlers hanging!

OTHER TIPS

It turns out I was not closing the downloaded file correctly. Let's all pretend this never happened.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top