Skip to content Skip to sidebar Skip to footer

Partial Upload With Storbinary In Python

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. Som

Solution 1:

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

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!

Post a Comment for "Partial Upload With Storbinary In Python"